代码之家  ›  专栏  ›  技术社区  ›  DJ'

Android内容提供商

  •  0
  • DJ'  · 技术社区  · 15 年前

    我想得到一个创建了数据库的内容提供者代码。我正在使用位于此处的工具tools/sqllite3.exe检查是否创建了数据库。

    请让我知道这件事的逐步程序…

    谢谢, -D

    2 回复  |  直到 12 年前
        1
  •  3
  •   Tseng    15 年前

    class MyDatabase extends SQLiteOpenHelper {
        public MyDatabase(Context context, dbName, null, version){
            super(context, dbName, null, version);
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            String createItemsTable = "create table mytable ("+
                "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                "_title TEXT NOT NULL, " +
                "_subtitle TEXT NULL " +
            " );";
    
            // Begin Transaction
            db.beginTransaction();
            try{
                // Create Items table
                db.execSQL(createItemsTable);
    
                // Transaction was successful
                db.setTransactionSuccessful();
            } catch(SQLException ex){
                Log.e(this.getClass().getName(), ex.getMessage(), ex);
            } finally {
                // End transaction
                db.endTransaction();
            }
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            String dropItemsTable = "DROP TABLE IF EXISTS mytable";
    
            // Begin transaction
            db.beginTransaction();
    
            try {
                if(oldVersion<2){
                    // Do your update/alter code here
                }
    
                db.setTransactionSuccessful();
            } catch(Exception ex){
                Log.e(this.getClass().getName(), ex.getMessage(), ex);
            } finally {
                // Ends transaction
                // If there was an error, the database won't be altered
                db.endTransaction();
            }
        }
    }
    

    MyDatabase myDb = new MyDatabase(getContext(),"databasename.db", null, 1); 
    

    推荐文章