How to create more than one table in sqlite in android?
I have three tables in my db. i try out many sample regarding creating db in sqlite, and it works out find most of them is one table only....
I try many ways like execute queries db.execSQL(query) to create each table in onCreate but it errors ..
SO Any good strategies to create more than one table??? should i create many class represent for each table extends SQLiteOpenHelper???
Thx
here is part of my class extends SQLiteOpenHelper
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try{
db.execSQL(CREATE_TABLE_1);
db.execSQL(CREATE_TABLE_2);
db.execSQL(CREATE_TABLE_3);
}catch( Exception e){
Log.e("dbAdapter", e.getMessage().toString());
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_1);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_2);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_3);
onCreate(db);
}
it seems the table2, and table3 not create cuz when i insert data into table 2 i get error - no such table2 found !!!
here my created table string:
//create script patient
public static String CREATE_TABLE_1= "create table " +
TABLE_USER + " (" +
USER_ID + " integer primary key autoincrement, " +
USER_NAME + " text not null, " +
DEVICE_KE开发者_如何学JAVAY + " text not null );" ;
public static String CREATE_TABLE_2= "create table " +
TABLE_GPS_SETTING + " (" +
SETTIN + " integer primary key autoincrement, " +
REFRESVAL + " long not null, " +
ODE + " varchar(20), " +
_TYPE + " varchar(20) );" ;
public static String CREATE_TABLE_3= "create table " +
TABLE_W + " (" +
GPD + " integer primary key autoincrement, " +
LITUDE + " double, " +
LITUDE + " double, " +
ATUDE + "integer,"+
M + "integer,"+
DTION + "integer,"+
DANCE + "integer,"+
LDD + "integer,"+
SESID + "double,"+
ACCCY + "double,"+
TMP + "long,"+
TATE + "TEXT,"+
D_ID + "TEXT);" ;
Any idea?
I believe there is no such thing as a varchar
in SQLite. Use text
.
You can create as many table as you want like this.
db.execSQL(TABLE1);
db.execSQL(TABLE2);
where TABLE1 is the string containing table creation command
Problem is you are trying to create table after creating the database.Uninstall your application and then run with the sql statements to create tables in the database.
精彩评论