创建于Android的SQLite表 [英] Creating SQLite Table in Android

查看:119
本文介绍了创建于Android的SQLite表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想值装入一个SQLite表上的机器人。我的应用程序不断地刷新,但它没有把我一个特定的错误,除了NullPointerException异常已被认为该表没有被正确创建。

I'm trying to load values into a SQLite table on android. My application keeps breaking but it doesnt throw me a specific error besides NullPointerException which has be believing the table is not being created correctly.

public class MySQLiteHelper{


        private static final String DATABASE_NAME = "breadcrumbs.db";
        private static final int DATABASE_VERSION = 1;
        public static final String TABLE_BREADCRUMBS = "breadcrumbs";
        private static Context context;
        static SQLiteDatabase db;
        private static SQLiteStatement insertStmt;
        private static final String INSERT ="insert into " + TABLE_BREADCRUMBS + " (time,lat,lon) values (?,?,?)";


        public MySQLiteHelper(Context context) {
            MySQLiteHelper.context = context;
            OpenHelper openHelper = new OpenHelper(MySQLiteHelper.context);
            MySQLiteHelper.db = openHelper.getWritableDatabase();
            MySQLiteHelper.insertStmt = MySQLiteHelper.db.compileStatement(INSERT);

        }

        public static long insert(long time,int lat,int lon) {
            insertStmt.bindLong(1, time);
            insertStmt.bindLong(2, lat);
            insertStmt.bindLong(3, lon);
            return insertStmt.executeInsert();
        }

        public void deleteAll() {
            db.delete(TABLE_BREADCRUMBS, null, null);
        }

        public List<String[]> selectAll()
        {
            List<String[]> list = new ArrayList<String[]>();
            Cursor cursor = db.query(TABLE_BREADCRUMBS, new String[] 
                    { "id","time","lat","lon"}, null, null, null, null, null); 
           int x=0;
            if (cursor.moveToFirst()) {
               do {
                    String[] b1=new String[]{cursor.getString(0),cursor.getString(1),cursor.getString(2),
    cursor.getString(3)};
                    list.add(b1);
                    x=x+1;
               } while (cursor.moveToNext());
            }
            if (cursor != null && !cursor.isClosed()) {
               cursor.close();
            } 
           cursor.close();
            return list;
       }

        public void delete(int rowId) {
            db.delete(TABLE_BREADCRUMBS, null, null); 
      } 


    private static class OpenHelper extends SQLiteOpenHelper {
            OpenHelper(Context context) {
                 super(context, DATABASE_NAME, null, DATABASE_VERSION);
            }
            public void onCreate(SQLiteDatabase db) {
                db.execSQL("CREATE TABLE " + TABLE_BREADCRUMBS + " (id INTEGER PRIMARY KEY, tim REAL, lat REAL, long REAL);");
                //db.execSQL("CREATE TABLE " + TABLE_BREADCRUMBS + " (id INTEGER PRIMARY KEY AUTOINCREMENT, time REAL, lat REAL, long REAL);");
            }

           @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
           {
                 db.execSQL("DROP TABLE IF EXISTS " + TABLE_BREADCRUMBS);
                 onCreate(db);


           }    
     }

和我打电话与另一个类中的方法:

And I'm calling the method in another class with:

    @Override
            public void onLocationChanged(Location location) {
                int lat = (int) (location.getLatitude() * 1E6);
                int lng = (int) (location.getLongitude() * 1E6);
                long time = (int)(location.getTime() * 1E6);
                GeoPoint point = new GeoPoint(lat, lng);
                createMarker();
                MySQLiteHelper.insert(time,lat,lng);
                mapController.animateTo(point); // mapController.setCenter(point);

            }
}

也许,我需要以某种方式调用OpenHelper?

Perhaps I need to call OpenHelper somehow?

推荐答案

通过改变插入语句来匹配列名添而不是时间固定。

Fixed by changing insert statement to match column name "tim" instead of "time".

这篇关于创建于Android的SQLite表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆