失败1:语法错误

时间:2013-12-07 18:19:50

标签: android sqlite

我是SQLite的新手。我想在Android中使用SQLite。但是当我运行它时,我遇到了这种错误。

错误:

  

准备'create tableuserInfoTable(_id integer primary key name text not nullemailtext not nullpasswordtext not nulltimetext not null);'时,在0x14d2c8上失败1(接近“tableuserInfoTable”:语法错误)。

代码:

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME="data";
    public static final int DATABASE_VERSION=1;

    public static final String USER_TABLE="userTable";
    public static final String C_ID="_id";
    public static final String USER="name";
    public static final String EMAIL="email";
    public static final String PASSWORD="password";
    public static final String TIME="time";

    public final String createDB="create table"+USER_TABLE+"("+C_ID+" integer primary key "
                             +USER+" text not null"  +EMAIL+ "text not null" +PASSWORD+ "text not null" 
                             +TIME+ "text not null);";

    public DBHelper(Context context)
    {
        super(context,DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(createDB);
    }


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    if (newVersion > oldVersion) {
        Log.w("MyAppTag","Updating database from version " + oldVersion + " to "
                + newVersion + " .Existing data will be lost.");
  db.execSQL("drop table if exists"+USER_TABLE);
  onCreate(db);
}
}  
 }

我从SO那里看了几乎每一个例子,但我无法弄明白。 谢谢

3 个答案:

答案 0 :(得分:3)

您的查询中没有逗号,并且您错过了一些空格。

尝试:

public final String createDB="create table "+USER_TABLE+"("+C_ID+" integer primary key, "
                         +USER+" text not null,"  +EMAIL+ " text not null, " +PASSWORD+ " text not null," 
                         +TIME+ " text not null);";

答案 1 :(得分:3)

试试这个

public final String createDB="create table "+USER_TABLE+"("
                         +C_ID+" integer primary key, "
                         +USER+" text not null,"  
                         +EMAIL+ " text not null," 
                         +PASSWORD+ " text not null," 
                         +TIME+ " text not null);";

一些错误:   - 你在table和tableName之间缺少一个空格;   - 你在列的末尾缺少逗号,   - 您还缺少列名和数据类型之间的空格:

您应该始终记录您的查询并尝试直接在数据库中运行它。错误将很快变得清晰。

答案 2 :(得分:0)

准备create tableuserInfoTable(_id integer primary key name text not nullemailtext not nullpasswordtext not nulltimetext not null);时,在0x14d2c8上失败1(接近“tableuserInfoTable”:语法错误)。

  • 此类错误缺少tablename(如果错误)或缺少逗号和空格或数据类型。

<强>解决方案 如果您的表名为tableuserInfoTable,则启动查询:

create table tableuserInfoTable(
_id integer primary key name text not null,
email text not null, 
password text not null,
time text not null);