开发者

Only first table in create table statement being created

开发者 https://www.devze.com 2023-01-01 03:45 出处:网络
The table \"credentials\" does show up in the adb shell.I\'ve checked logcat and it doesn\'t seem to report a problem...

The table "credentials" does show up in the adb shell. I've checked logcat and it doesn't seem to report a problem...

   private static final String DATABASE_CREATE =
        "create table credentials (_id integer primary key autoincrement, "
                + "username text not null, password text not null, "
                + "lastupdate text);"
      + "create table user (_id integer primary key autoincrement, "
                + "firstname text not null, "
                + "lastname text not null);"
      + "create table phone (_phoneid integer primary key autoincrement,开发者_Go百科 "
                + "userid integer not null, phonetype text not null, "
                + "phonenumber text not null);"
      + "create table email (_emailid integer primary key autoincrement, "
                + "userid integer not null, emailtype text not null, "
                + "emailaddress text not null);"
      + "create table address (_addressid integer primary key autoincrement,"
                + "userid integer not null, addresstype text not null, "
                + "address text not null);"
      + "create table instantmessaging (_imid integer primary key autoincrement, "
                + "userid integer not null, imtype text not null, "
                + "imaccount text not null);";

I've been pouring over this and I bet its some silly syntax typo! Or, at least I hope it is something trivial ;-)


I suppose that you are using :

yourDB.execSQL("your statement");

If so, the google documentation mentions this :

Execute a single SQL statement that is not a query. For example, CREATE TABLE, DELETE, INSERT, etc. Multiple statements separated by ;s are not supported. it takes a write lock

So you have to fragment each create table statement and repeat the query for each table.


If I recall correctly, I encountered a similar problem and discovered that only 1 statement is executed per call to execSQL() or similar methods. Any extra statements are silently ignored.

Try separating each statement into separate strings and executing them separately, rather than a single string and single call.

For example:

private static final String TABLE_1 =
    "create table credentials (_id integer primary key autoincrement, "
    + "username text not null, password text not null, "
    + "lastupdate text);";

private static final String TABLE_2 =
    "create table user (_id integer primary key autoincrement, "
    + "firstname text not null, "
    + "lastname text not null);";

private static final String TABLE_3 =
    "create table phone (_phoneid integer primary key autoincrement, "
    + "userid integer not null, phonetype text not null, "
    + "phonenumber text not null);";

private static final String TABLE_4 =
    "create table email (_emailid integer primary key autoincrement, "
    + "userid integer not null, emailtype text not null, "
    + "emailaddress text not null);";

private static final String TABLE_5 =
    "create table address (_addressid integer primary key autoincrement,"
    + "userid integer not null, addresstype text not null, "
    + "address text not null);";

private static final String TABLE_6 = 
    "create table instantmessaging (_imid integer primary key autoincrement, "
    + "userid integer not null, imtype text not null, "
    + "imaccount text not null);";

public void createTables(){
    db.execSQL(TABLE_1);
    db.execSQL(TABLE_2);
    db.execSQL(TABLE_3);
    db.execSQL(TABLE_4);
    db.execSQL(TABLE_5);
}
 db.execSQL(TABLE_6);


put Go after each Create Table statement

Updated script

private static final String DATABASE_CREATE =
        "create table credentials (_id integer primary key autoincrement, "
                + "username text not null, password text not null, "
                + "lastupdate text); Go;"
      + "create table user (_id integer primary key autoincrement, "
                + "firstname text not null, "
                + "lastname text not null); Go;"
      + "create table phone (_phoneid integer primary key autoincrement, "
                + "userid integer not null, phonetype text not null, "
                + "phonenumber text not null); Go;"
      + "create table email (_emailid integer primary key autoincrement, "
                + "userid integer not null, emailtype text not null, "
                + "emailaddress text not null) Go;;"
      + "create table address (_addressid integer primary key autoincrement,"
                + "userid integer not null, addresstype text not null, "
                + "address text not null); Go;"
      + "create table instantmessaging (_imid integer primary key autoincrement, "
                + "userid integer not null, imtype text not null, "
                + "imaccount text not null); Go;";
0

精彩评论

暂无评论...
验证码 换一张
取 消