Android SQLite插入

时间:2013-05-14 00:20:03

标签: android sqlite

我有以下表格配置

    package com.lynas.entertainmenttracker;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBConnectionTableMovie {
    public static final String DBNAME = "entertainment_tracker";
    public static final String DBTABLE = "movie";
    public static final int VERSION = 1;

    public static final String DBCREATE_MOVIE = "CREATE TABLE movie(" +
            "movie_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
            "movie_name VARCHAR, " +
            "movie_release_date date, " +
            "movie_extra VARCHAR)";

    private final Context context;
    private DBHelper dbh;
    private SQLiteDatabase db;

    public DBConnectionTableMovie(Context ctx){

        this.context = ctx;
        dbh = new DBHelper(context);
    }


    private static class DBHelper extends SQLiteOpenHelper{

        DBHelper(Context context){
            super(context, DBNAME,null,VERSION);
        }


        @Override
        public void onCreate(SQLiteDatabase db) {
            //try{
                db.execSQL(DBCREATE_MOVIE);
            //}catch(SQLException e){
                //e.printStackTrace();
            //}

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE movie IF EXISTS");
            onCreate(db);

        }

    }
    public DBConnectionTableMovie open() throws SQLException{
        db = dbh.getWritableDatabase();
        return this;
    }

    public void close() throws SQLException{
        dbh.close();
    }

    public void addANewRow(String inserQuery){
        db.execSQL(inserQuery);
    }
    public void updateRow(String updateQuery){
        db.execSQL(updateQuery);
    }
    public void deleteRow(String deleteQuery){
        db.execSQL(deleteQuery);
    }
    public Cursor selectRow(String selectQuery){
        return db.rawQuery(selectQuery, null);
    }


}

我不确定这是否相关,但我为另一个表使用了相同的数据库名称 public static final String DBNAME =“entertainment_tracker”;

我试图通过创建该类的对象使用以下查询插入一行。它在一个活动中因此使用它作为构造函数

DBConnectionTableMovie dbcon = new DBConnectionTableMovie(this);
String insertANewMovie = "INSERT INTO movie VALUES (NULL, 'name', '2013-05-01', 'empty')";

dbcon.open();
dbcon.addANewRow(insertANewMovie);
dbcon.close();

我得到的表不存在错误。有人能告诉我什么错了吗?

错误日志

05-14 00:24:18.366:E /数据库(856):在准备'INSERT INTO movie VALUES(NULL,'jack','2013-05-14)时,在0x259138上失败1(没有这样的表:电影) ',''空')'。

0 个答案:

没有答案