将数据插入SQLite时出错

时间:2013-11-28 08:21:03

标签: android sqlite syntax insert android-sqlite

使用@hotmail

插入数据时插入数据时出现问题

这是错误

11-28 08:07:19.684: E/CREATE TABLE ERROR(21840): android.database.sqlite.SQLiteException: near "@hotmail": syntax error: , while compiling: INSERT INTO user_table SELECT han AS userName,  123 AS password,  han@hotmail.com AS emailAddress,  0123456678 AS phoneNuber,  50.00 AS balance  UNION SELECT felicia, 123, felicia@hotmail.com, 0123456678, 100.00

但是当我删除@hotmail时,我遇到了另一个没有这样的列的问题

11-28 08:16:06.704: E/CREATE TABLE ERROR(24403): android.database.sqlite.SQLiteException: no such column: felicia: , while compiling: INSERT INTO user_table SELECT han AS userName,  123 AS password,  han AS emailAddress,  0123456678 AS phoneNuber,  50.00 AS balance  UNION SELECT felicia, 123, felicia, 0123456678, 100.00

这是我的表

String createuser = "CREATE TABLE user_table (_id integer primary key autoincrement not null, "
                + "userName text,"
                + "password text,"
                + "emailAddress text,"
                + "phoneNumber text,"
                + "balance text);";

这是我的数据插页

String data = " INSERT INTO user_table SELECT han AS userName, "
                + " 123 AS password, "
                + " han@hotmail.com AS emailAddress, "
                + " 0123456678 AS phoneNuber, "
                + " 50.00 AS balance "
                + " UNION SELECT felicia, 123, felicia@hotmail.com, 0123456678, 100.00 ";

        try
        {
            db.execSQL(createuser);
            db.execSQL(data);
        }
        catch(Exception e)
        {
            Log.e("CREATE TABLE ERROR", e.toString());
            e.printStackTrace();
        }

2 个答案:

答案 0 :(得分:2)

呸。作为[潜在]开发人员,您期望自行解决语法问题。

这里有两个重要的步骤

  1. 建立一个可以轻松测试语法/行为的环境
  2. 理解(并积极学习)语法。
  3. 对于初学者,让我们使用数据库命令行界面。可以在PC上使用sqlite3.exe,也可以在Android上使用(有几个!)或SQLFiddle之类的内容。我将使用SQLFiddle,以便我们都可以参与,whoohoo!

    接下来的一点是理解语法。 SQLite INSERT的语法与图形化列车轨道完美配合。 (免费赠品:ANSI SQL语法中的文本文字写成'字符串'。)

    话虽如此,请参阅this fiddle。随意玩吧。

    CREATE TABLE user_table (
      _id integer primary key autoincrement not null, 
      userName text,
      password text,
      emailAddress text,
      phoneNumber text,
      balance text);
    
    -- INSERT from a select
    -- This REQUIRES that the column as specified after the table name
    insert into user_table (userName, password, emailAddress, phoneNumber, balance)
    select
      'han' as userName,
       123 AS password,
      'han@hotmail.com' AS emailAddress,
      '0123456678' AS phoneNuber,
      50.00 AS balance;
    
    -- Insert from VALUES, if they match and are in-order, do not
    -- need the column names.
    -- Insert NULL into _id to get the auto-increment behavior.
    insert into user_table
    values (NULL, 'han2', 123, 'han@hotmail.com', '0123456678', 50.00);
    
    -- Multi-insert from SELECT (could also be done with VALUES)
    insert into user_table (userName, password, emailAddress, phoneNumber, balance)
    select
      'han3' as userName,
       123 AS password,
      'han@hotmail.com' AS emailAddress,
      '0123456678' AS phoneNuber,
      50.00 AS balance
    union select
      'han4' as userName,
       123 AS password,
      'han@hotmail.com' AS emailAddress,
      '0123456678' AS phoneNuber,
      50.00 AS balance
    

    现在,虽然这适用于直接SQL查询,但在将工作查询移植到Android时,您需要适应占位符 - 即,SQL命令看起来像insert into user_table values (NULL, ?, ?, ?, ?, ?)(其中每个?是一个占位符),然后值数组将提供给使用SQL字符串的相应方法调用

    Android还支持像SQLiteDatabase.insert这样的基本形式,它们非常有用,可以处理很多“常见的垃圾”。 execSQL的文档建议使用execSQL进行基本的INSERT / UPDATE / DELETE操作,这对于常见场景是很好的建议 - 在单个事务中多次调用insert将是从代码维护的角度来看,“好”(也许更好,从代码维护的角度来看)作为更复杂的手动多插入。

答案 1 :(得分:0)

Refer the docs to learn about sqlite queries

您的语法错误,在引号'TEXT GOES HERE'中插入文字而您错过了,