1

我在android中创建了这个表:

     String CREATE_TABLE="CREATE TABLE " + db_NAME + " ("
                        + row_ID + " INT PRIMARY KEY AUTOINCREMENT,"
                        + row_NAME + " TEXT, "
                        + row_EMAIL + " TEXT, "
                        + row_WEBSITE + " TEXT, "
                        + row_TELEPHONE1 + " TEXT, "
                        + row_TELEPHONE2 + " TEXT, "
                        + row_TELEPHONE3 + " TEXT, "
                        + row_TELEPHONE4 + " TEXT, "
                        + row_TELEPHONE5 + " TEXT;";
    db_name.execSQL(CREATE_TABLE);

我正在尝试插入以下数据:

姓名:阿拉姆·侯赛因

电子邮件:ahlam@ahlam.ahlam

网址:www.ahlam.com

电话#1:123456789

电话#2:0987654321

电话#3:

电话#4:

电话#5:

使用以下字符串:

     String sql_entry = String.format("Insert into %s (%s, %s, %s, %s, %s, %s, %s, %s) values (%s, %s, %s, %s, %s, %s, %s, %s);",tab_NAME, row_NAME, row_EMAIL, row_WEBSITE, row_TELEPHONE1, row_TELEPHONE2, row_TELEPHONE3, row_TELEPHONE4, row_TELEPHONE5, name, email, website, telephone1, telephone2, telephone3,telephone4, telephone5);
     ourDatabase.execSQL(sql_entry);

其中:( row_NAME, row_EMAIL, row_WEBSITE, row_TELEPHONE1, row_TELEPHONE2, row_TELEPHONE3, row_TELEPHONE4, row_TELEPHONE5)是列名'

和 :(name, email, website, telephone1, telephone2, telephone3,telephone4, telephone5)是要插入数据库的数据

当我运行应用程序时,我收到一条错误消息:

      SQLiteLog        (1) near "M" : syntax error

那么问题是什么???

4

2 回答 2

1

您遗漏了'字符以包含字符串...

查询应为:

Insert into %s (%s, %s, %s, %s, %s, %s, %s, %s) values ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');

所以创建查询字符串本身应该如下所示:

 String sql_entry = String.format("Insert into %s (%s, %s, %s, %s, %s, %s, %s, %s) values ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');",
tab_NAME, row_NAME, row_EMAIL, row_WEBSITE, row_TELEPHONE1, row_TELEPHONE2, row_TELEPHONE3, row_TELEPHONE4, row_TELEPHONE5, 
 name, email, website, telephone1, telephone2, telephone3,telephone4, telephone5);

顺便说一句,在开发应用程序时,我总是在不使用框架的情况下记录我创建的 SQL 查询——很容易犯这样的错误。

于 2012-12-21T13:44:30.023 回答
1

用单引号将非数字值括起来。

String sql_entry = String.format("Insert into %s (%s, %s, %s, %s, %s, %s, %s, %s)"
 +" values ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
  tab_NAME, row_NAME, row_EMAIL, row_WEBSITE, row_TELEPHONE1, row_TELEPHONE2, 
  row_TELEPHONE3, row_TELEPHONE4, row_TELEPHONE5, name, email, website, 
  telephone1, telephone2, telephone3,telephone4, telephone5);
于 2012-12-21T13:44:31.447 回答