android:在sqlite中创建表时出错?

时间:2013-01-17 13:07:08

标签: android sqlite

我在android中创建了应用程序,我执行了crud操作。在应用程序数据库文件中成功创建,但在创建表及其后的错误是Logcat错误和数据库适配器类。

Logcat错误

01-17 13:03:59.139: E/SQLiteLog(1036): (1) table timerecordsnew1 has no column named note
01-17 13:03:59.309: E/SQLiteDatabase(1036): Error inserting note=asdfsdfadf
01-17 13:03:59.309: E/SQLiteDatabase(1036): android.database.sqlite.SQLiteException: table timerecordsnew1 has no column named note (code 1): , while compiling: INSERT INTO timerecordsnew1(note) VALUES (?)
01-17 13:03:59.309: E/SQLiteDatabase(1036):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)

以下是我的数据库适配器类

public class TimeTrackerAdapter {

    private static final int DATABASE_VERSION = 4;
    private static final String DATABASE_NAME = "timetrackernew1.db";
    //Table
    private static final String TABLE_NAME = "timerecordsnew1";

    //Table Column Variable
    public static final String TIMERECORDS_COLUMN_ID = "id";
    public static final String TIMERECORDS_COLUMN_TIME = "time";
    public static final String TIMERECORDS_COLUMN_NOTE = "note";

    private TimeTrackerOpenHelper openHelper;
    private SQLiteDatabase sqlDb;

    //Database creation sql statement
    private static final String SQL_CREATE_ENTRIES = "CREATE TABLE " +  
            TABLE_NAME + "(" + TIMERECORDS_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
            TIMERECORDS_COLUMN_TIME + " TEXT " + TIMERECORDS_COLUMN_NOTE + " TEXT);";

    public TimeTrackerAdapter(Context context) {
        openHelper = new TimeTrackerOpenHelper(context);
        sqlDb = openHelper.getWritableDatabase();
    }

    //to insert data into database
    public void saveRecords(String t, String n) {

        ContentValues contentValues = new ContentValues();
        contentValues.put(TIMERECORDS_COLUMN_NOTE,t);
        contentValues.put(TIMERECORDS_COLUMN_NOTE, n);

        sqlDb.insert(TABLE_NAME, null, contentValues);
    }
    //get fetch the all data
    public Cursor getAllRecords() {

        return sqlDb.rawQuery("select * from " + TABLE_NAME, null);

    }

    // inner class
    private class TimeTrackerOpenHelper extends SQLiteOpenHelper {

        public TimeTrackerOpenHelper(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
            System.out.println("create statement"+SQL_CREATE_ENTRIES);
            try
            {
                db.execSQL(SQL_CREATE_ENTRIES);

            }catch(SQLiteException sql)
            {
                sql.printStackTrace();
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
        }
    }

}

请有人帮我解决这个错误。

感谢你

4 个答案:

答案 0 :(得分:1)

您错过了两列中SQL的逗号:

private static final String SQL_CREATE_ENTRIES = "CREATE TABLE " +  
        TABLE_NAME + "(" + TIMERECORDS_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
        TIMERECORDS_COLUMN_TIME + " TEXT **,%Comma goes here%** " + TIMERECORDS_COLUMN_NOTE + " TEXT);";

答案 1 :(得分:0)

TIMERECORDS_COLUMN_TIME + " TEXT " == TIMERECORDS_COLUMN_TIME + " TEXT, "

你忘了“,”char。

下次阅读异常并更准确地查看代码。

答案 2 :(得分:0)

看起来你错过了create table DDL中的逗号。 (在TIMERECORDS_COLUMN_TIME栏上..

从:

   private static final String SQL_CREATE_ENTRIES = 
        "CREATE TABLE " + TABLE_NAME + "(" + 
           TIMERECORDS_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
           TIMERECORDS_COLUMN_TIME + " TEXT " + 
           TIMERECORDS_COLUMN_NOTE + " TEXT);";

   private static final String SQL_CREATE_ENTRIES = 
         "CREATE TABLE " + TABLE_NAME + "(" + 
            TIMERECORDS_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
            TIMERECORDS_COLUMN_TIME + " TEXT, " + 
            TIMERECORDS_COLUMN_NOTE + " TEXT);";

答案 3 :(得分:0)

下面:

        TIMERECORDS_COLUMN_TIME + " TEXT " + TIMERECORDS_COLUMN_NOTE + " TEXT);";

你得到"时间文字说明文字"没有逗号分隔它们。

Sqlite没有将其检测为语法错误:它以"无类型"数据库,然后添加了对类型亲和力的支持,但继续允许所有类型的垃圾进行列类型规范。