在我的数据库中添加数据时出错

时间:2017-07-31 15:30:17

标签: android database sqlite android-sqlite

我正在尝试配置一个简单的SQLiteOpenHelper,并在执行时收到错误。 android.database.sqlite.SQLiteException:table people_table没有名为Minutes(code 1)的列:,编译时:INSERT INTO people_table(Minutes,Hour)VALUES(?,?) 我能搞清楚是什么问题?

  package radiofm.arabel;

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


public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String TAG = "DatabaseHelper";

    private static final String TABLE_NAME = "people_table";
    private static final String COL1 = "ID";
    private static final String COL2 = "Hour";
    private static final String COL3 = "Minutes";



    public DatabaseHelper(Context context) {
        super(context, TABLE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String createTable = "CREATE TABLE " + TABLE_NAME + " (" +

                COL1 + "ID INTEGER PRIMARY KEY AUTOINCREMENT," +

                COL2 + "TEXT," +

                COL3 + "TEXT "  +

                ");";

        db.execSQL(createTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    public boolean addData(String item1,String item2) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL2, item1);
        contentValues.put(COL3, item2);

        Log.d(TAG, "addData: Adding " + item1 + " to " + TABLE_NAME);


        long result = db.insert(TABLE_NAME, null, contentValues);

        //if date as inserted incorrectly it will return -1
        if (result == -1) {
            return false;
        } else {
            return true;
        }
    }


}

2 个答案:

答案 0 :(得分:1)

创建表时忘记了空间。您的列现在名为(define (remove x ls) (if (null? ls) '() (let ((h (car ls))) (if (eqv? x h) ; using `equal?` would be a better idea (remove x (cdr ls)) (cons h (remove x (cdr ls)))))))

这本身不是错误,因为您不必在SQLite中指定列类型。

Reference

要修复它,请确保删除当前数据库并用此替换Create table query。

MinutesTEXT

答案 1 :(得分:0)

onCreate 似乎缺少SQL列定义中的空格。此外,您的id列必须定义为_id。

试试这个。

core

 private static final String COL1 = "_id";