【问题标题】:Android - How to Create Sqlite database with Tables [duplicate]Android - 如何使用表创建 Sqlite 数据库
【发布时间】:2013-04-27 12:08:54
【问题描述】:

我需要创建一个包含 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);
      }
}

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

DatabaseUtils db = new DatabaseUtils(this);

但是数据库不是用这 3 个表创建的。我究竟做错了什么?顺便说一句,我所有的字符串值都正确。请帮我如何创建数据库。

【问题讨论】:

  • 您的DATABASE_VERSIONnumber 是在哪里定义的?将其设置为更高的值,因为如果我认为值发生变化,它只会在onCreate 中创建新表 - 也许您之前已经执行过该类(没有表)。编辑:另外你应该在调用 super 之前定义你的DATABASE_PATH - 因为你应该在那里传递数据库的这个名称(或/和路径)。

标签: java android sqlite


【解决方案1】:

我找到了解决方案。 DatabaseUtils' onCreate() 如果我像下面这样实现,则永远不会被调用:

 DatabaseUtils db = new DatabaseUtils(this);

在myActivity 的onCreate() 方法中。我需要在 myActivity 中调用getWritableDatabase(),如下所示:

DatabaseUtils db = new DatabaseUtils(this);
db.getWritableDatabase();

然后将调用DatabaseUtils'onCreate() 并创建表。

【讨论】:

    【解决方案2】:

    private void 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);      
    }
    

    这是一个供你参考的例子

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 2018-06-23
      相关资源
      最近更新 更多