android - 失败1 :Syntax error

标签 android sqlite

我是 SQLite 新手。我正在尝试在 Android 中使用 SQLite。但是我运行的时候就遇到这样的错误。

错误:

Failure 1 (near "tableuserInfoTable": syntax error) on 0x14d2c8 when preparing 'create tableuserInfoTable(_id integer primary key name text not nullemailtext not nullpasswordtext not nulltimetext not null);'.

代码:

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中的每一个例子,但我无法弄清楚。 谢谢

最佳答案

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

尝试:

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

关于android - 失败1 :Syntax error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20444838/

相关文章:

Android DP 到 PX 的转换,在 mdpi 上 160 dp 总是 160 px 吗?

android - 无法在 Android 中添加电子邮件签名

java - 将 MySQL 转储文件导入到 SQLite

Android SQLite 条件在轮类期间失败更改

android - 如何指定正确的SQLiteDatabase.query返回类型

android - android studio3.x : build project failed 问题

java - 在运行时检查位置设置更改

SQL - 插入一行并返回主键

android - 旋转动画在 android 4.0 中不起作用

django - TypeError:“ModelBase”对象不支持索引。