-3

我在 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

4 回答 4

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);";
于 2013-01-17T13:09:24.463 回答
0

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

你忘记了“,”字符。

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

于 2013-01-17T13:09:46.653 回答
0

看起来您在create tableDDL 中缺少逗号。(在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);";
于 2013-01-17T13:13:01.840 回答
0

这里:

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

你会得到“time TEXT note TEXT”,没有逗号来分隔它们。

Sqlite 不会将其检测为语法错误:它以“无类型”数据库开始,然后添加了对type affinity的支持,但继续允许所有类型的垃圾用于列类型规范。

于 2013-01-17T13:13:36.270 回答