这个SQLite Create Statement有什么问题

时间:2014-05-08 05:53:56

标签: android android-sqlite

我无法使用下面的SQL语句在Android项目中创建SQLite表,这个Sqlite语句我做错了什么?

public class CustomerSQLiteHelper extends SQLiteOpenHelper {

    private static final String DBNAME = "Customer.db";
    private static final int DBVERSION = 1;     


    //Create constants for column names of the Customer Table
    private final static String TABLE_NAME = "custTable";
    private final static String COLUMN_ID = "_id";
    private final static String COLUMN_NAME = "name";
    private final static String COLUMN_EMAIL = "email";
    private final static String COLUMN_PHONE = "phone";
    private final static String COLUMN_UID = "card_uid2";
    private final static String COLUMN_DATE_CREATED = "enrollment_date";
    private final static String COLUMN_LAST_UPDATED = "last_transaction_date";
    private final static String COLUMN_INITIAL_VALUE = "initial_value";
    private final static String COLUMN_CURRENT_VALUE = "current_value";  


    private static final String CREATE_CUSTOMER_TABLE =
                "CREATE TABLE " + TABLE_NAME + "(" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "  +
                COLUMN_NAME + " TEXT NOT NULL, " +
                COLUMN_EMAIL + " TEXT, " +
                COLUMN_PHONE + " TEXT, " +
                COLUMN_UID + " TEXT, " +
                COLUMN_DATE_CREATED + " TIMESTAMP NOT NULL DEFAULT, " +
                COLUMN_LAST_UPDATED + " TIMESTAMP NOT NULL DEFAULT, " +
                COLUMN_INITIAL_VALUE + " NUMERIC, " +
                COLUMN_CURRENT_VALUE + " NUMERIC " +
                ")";            

    public CustomerSQLiteHelper(Context context){
        super(context, DBNAME, null, DBVERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_CUSTOMER_TABLE);  
        android.util.Log.i(TAG, "Table created");

    }

它在仿真器中失败,并显示以下错误消息,从LogCat获取

05-08 00:14:41.435: E/SQLiteLog(2434): (1) near ",": syntax error
05-08 00:14:41.435: D/AndroidRuntime(2434): Shutting down VM
05-08 00:14:41.435: W/dalvikvm(2434): threadid=1: thread exiting with uncaught exception (group=0xb1f11648)
05-08 00:14:41.435: E/AndroidRuntime(2434): FATAL EXCEPTION: main
05-08 00:14:41.435: E/AndroidRuntime(2434): java.lang.RuntimeException: Unable to resume activity {com.txsystems.valuecard/com.txsystems.valuecard.app.EnrollActivity}: android.database.sqlite.SQLiteException: near ",": syntax error (code 1): , while compiling: CREATE TABLE custTable(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT, phone TEXT, card_uid2 TEXT, enrollment_date TIMESTAMP NOT NULL DEFAULT, last_transaction_date TIMESTAMP NOT NULL DEFAULT, initial_value NUMERIC, current_value NUMERIC )

3 个答案:

答案 0 :(得分:4)

当您使用Default关键字时,您还需要提供default值,这是您在查询中遗漏的。

答案 1 :(得分:3)

如果您使用DEFAULT,则必须在关键字后提供默认值,而不是,

更改为

private static final String CREATE_CUSTOMER_TABLE =
            "CREATE TABLE " + TABLE_NAME + "(" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "  +
            COLUMN_NAME + " TEXT NOT NULL, " +
            COLUMN_EMAIL + " TEXT, " +
            COLUMN_PHONE + " TEXT, " +
            COLUMN_UID + " TEXT, " +
            COLUMN_DATE_CREATED + " TIMESTAMP NOT NULL DEFAULT 0, " +
            COLUMN_LAST_UPDATED + " TIMESTAMP NOT NULL DEFAULT 0, " +
            COLUMN_INITIAL_VALUE + " NUMERIC, " +
            COLUMN_CURRENT_VALUE + " NUMERIC " +
            ")";          

答案 2 :(得分:2)

您需要为DEFAULT指定一个值

private static final String CREATE_CUSTOMER_TABLE =
                "CREATE TABLE " + TABLE_NAME + "(" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "  +
                COLUMN_NAME + " TEXT NOT NULL, " +
                COLUMN_EMAIL + " TEXT, " +
                COLUMN_PHONE + " TEXT, " +
                COLUMN_UID + " TEXT, " +
                COLUMN_DATE_CREATED + " TIMESTAMP NOT NULL DEFAULT '', " +   <---
                COLUMN_LAST_UPDATED + " TIMESTAMP NOT NULL DEFAULT '', " +   <---
                COLUMN_INITIAL_VALUE + " NUMERIC, " +
                COLUMN_CURRENT_VALUE + " NUMERIC " +
                ")";