当前位置:  开发笔记 > 数据库 > 正文

只创建了create table语句中的第一个表

如何解决《只创建了createtable语句中的第一个表》经验,为你挑选了2个好方法。

表"凭据"确实显示在adb shell中.我检查过logcat,它似乎没有报告问题......

   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, "
                + "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);";

我一直在倾倒这个,我打赌它有些愚蠢的语法错字!或者,至少我希望这是微不足道的;-)



1> Sephy..:

我想你正在使用:

yourDB.execSQL("your statement");

如果是这样,谷歌文档提到:

执行不是查询的单个SQL语句.例如,CREATE TABLE,DELETE,INSERT等.不支持由; s分隔的多个语句.它需要一个写锁定

因此,您必须对每个create table语句进行分段,并对每个表重复查询.



2> chrisbunney..:

如果我没记错的话,我遇到了类似的问题,并发现每次调用execSQL()或类似方法只执行1条语句.任何额外的陈述都会被默默忽略.

尝试将每个语句分成单独的字符串并单独执行,而不是单个字符串和单个调用.

例如:

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

推荐阅读
手机用户2402852387
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有