简体   繁体   English

如何在 android 中的 sqlite 中创建多个表?

[英]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....我尝试了许多关于在 sqlite 中创建 db 的示例,结果发现其中大多数只有一张表....

I try many ways like execute queries db.execSQL(query) to create each table in onCreate but it errors..我尝试了很多方法,例如执行查询 db.execSQL(query) 以在 onCreate 中创建每个表,但它出错了..

SO Any good strategies to create more than one table???所以有什么好的策略来创建多个表吗??? should i create many class represent for each table extends SQLiteOpenHelper???我应该为每个表创建许多 class 代表扩展 SQLiteOpenHelper 吗?

Thx谢谢

here is part of my class extends SQLiteOpenHelper这是我的 class 扩展 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 !!!当我将数据插入表 2 时,似乎 table2 和 table3 没有创建 cuz 我得到错误 - 没有找到这样的 table2 !!!

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_KEY + " 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?任何想法?

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其中 TABLE1 是包含表创建命令的字符串

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.问题是您在创建数据库后尝试创建表。卸载您的应用程序,然后使用 sql 语句运行以在数据库中创建表。

I believe there is no such thing as a varchar in SQLite .我相信SQLite 中没有varchar这样的东西 Use text .使用text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM