2013-05-03 96 views
3

我需要用3個表創建一個數據庫SQLite數據庫和我做這個象下面這樣:Android的 - 如何創建具有表

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

,並在我的活動我打電話如下DatabaseUtilsonCreate

DatabaseUtils db = new DatabaseUtils(this); 

但數據庫不是用3個表創建的。我究竟做錯了什麼?順便說一句,我有正確的所有字符串值。請幫助我如何創建數據庫。

+0

?將其設置爲更高的值,因爲如果值改變,我只能在'onCreate'中創建新表 - 也許你已經在之前執行過類(沒有表)。 編輯:你也應該在調用super之前定義你的'DATABASE_PATH' - 因爲你應該在那裏傳遞這個數據庫的名字(或者/和路徑)。 – 2013-05-03 06:37:56

回答

5

我找到了解決方案。小號onCreate()方法:DatabaseUtils '在myActivity

DatabaseUtils db = new DatabaseUtils(this); 

onCreate()如果我實現像下面是從來沒有所謂的'。我需要調用在myActivity getWritableDatabase()如下:

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

然後DatabaseUtilsonCreate()將被調用,創建表。

0

私人無效db_ini_create_table(android.database.sqlite.SQLiteDatabase分貝){

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

此僅供參考一個例子,其中是你的`DATABASE_VERSION`number定義