【问题标题】:Only first table in create table statement being created仅创建表语句中的第一个表
【发布时间】:2010-05-28 10:21:16
【问题描述】:

“凭据”表确实显示在 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);";

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

【问题讨论】:

    标签: android sqlite


    【解决方案1】:

    我想你正在使用:

    yourDB.execSQL("your statement");
    

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

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

    因此,您必须将每个 create table 语句分段并为每个表重复查询。

    【讨论】:

    • 并且不允许使用分号
    【解决方案2】:

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

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

    例如:

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

    【讨论】:

      【解决方案3】:

      在每个 Create Table 语句之后放置 Go

      更新脚本

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

      【讨论】:

      • 然后尝试中断每个语句而不是执行并查看结果,因为其中一个语句可能会导致异常
      猜你喜欢
      • 1970-01-01
      • 2020-08-02
      • 1970-01-01
      • 2011-07-21
      • 1970-01-01
      • 1970-01-01
      • 2018-08-05
      • 2014-01-20
      • 1970-01-01
      相关资源
      最近更新 更多