Android - 如何创建带有表的SQLite数据库

3

我需要创建一个包含3个表的数据库,以下是我的实现:

public class DatabaseUtils extends SQLiteOpenHelper {
      private final Context myContext;  
      private SQLiteDatabase DataBase; 

      // Database creation sql statement
      private static final String CREATE_TABLE_CS = "create table "+ TABLE_CS + "(" + COLUMN_CS + " TEXT NOT NULL, " + COLUMN_CE_CID + " INTEGER NOT NULL, "+ COLUMN_CE_PID +" INTEGER NOT NULL);";
      private static final String CREATE_TABLE_SS = "create table "+ TABLE_SS + "(" + COLUMN_SS + " TEXT NOT NULL, " + COLUMN_SUB_CID + " INTEGER NOT NULL, "+ COLUMN_SUB_PID +" INTEGER NOT NULL);";
      private static final String CREATE_TABLE_AS = "create table "+ TABLE_AS + "(" + COLUMN_AS + " TEXT NOT NULL, " + COLUMN_CID + " INTEGER NOT NULL, "+ COLUMN_AID +" INTEGER NOT NULL);";

      public DatabaseUtils(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        DATABASE_PATH =  Environment.getDataDirectory().getAbsolutePath() + "/" +"data/"+ context.getResources().getString(R.string.app_package);
        this.myContext = context;
      }

      @Override
      public void onCreate(SQLiteDatabase database) {
        database.execSQL(CREATE_TABLE_CS);
        database.execSQL(CREATE_TABLE_SS);
        database.execSQL(CREATE_TABLE_AS);
      }
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(DatabaseUtils.class.getName(),"Upgrading database from version " + oldVersion + " to "+ newVersion + ", which will destroy all old data");
        //db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS);
        onCreate(db);
      }
}

在我的Activity中,我在onCreate中调用DatabaseUtils类,如下所示:

DatabaseUtils db = new DatabaseUtils(this);

但是数据库没有创建3个表格。我做错了什么?顺便说一下,我所有的字符串值都是正确的。请帮助我如何创建数据库。


你的 DATABASE_VERSION 数字定义在哪里?将其设置为更高的值,因为只有在该值发生变化时才会在 onCreate 中创建新表 - 也许你之前已经执行过该类(没有表)。另外,你应该在调用 super 之前定义你的 DATABASE_PATH - 因为你需要将数据库的名称(或路径)传递给它。 - OschtärEi
2个回答

5
我找到了解决方案。如果我像下面这样实现,DatabaseUtilsonCreate()永远不会被调用:
 DatabaseUtils db = new DatabaseUtils(this);

在我的Activity的onCreate()方法中,我需要在myActivity中调用getWritableDatabase(),如下所示:
DatabaseUtils db = new DatabaseUtils(this);
db.getWritableDatabase();

然后会调用 DatabaseUtilsonCreate() 方法来创建表。


0

私有空白 db_ini_create_table(android.database.sqlite.SQLiteDatabase db) {

    String str_sql = "create table [possible_know_persions]         ("+
                                                                        "[id]                       INTEGER         PRIMARY KEY AUTOINCREMENT               NOT NULL,"+
                                                                        "[adate]                    DATETIME        DEFAULT (datetime('now','localtime'))   NOT NULL,"+
                                                                        "[current_userid]           INTEGER         DEFAULT 0 NOT NULL                              ,"+
                                                                        "[stranger_user_id]         INTEGER         DEFAULT 0 NOT NULL                              ,"+
                                                                        "[common_friend_count]      INTEGER         DEFAULT 0 NOT NULL                              ,"+
                                                                        "[common_game_count]        INTEGER         DEFAULT 0 NOT NULL                              ,"+
                                                                        "[user_account_type]        INTEGER         DEFAULT 0 NOT NULL                              ,"+
                                                                        "[verify_type]              INTEGER         NULL                                             "+
                                                                    ");";db.execSQL(str_sql); 
    str_sql = "CREATE INDEX [possible_stranger_user_id]     on [possible_know_persions] ("+"[stranger_user_id] asc "+");";db.execSQL(str_sql);      
}

这是一个供您参考的示例


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接