麻烦哪位大神帮我看一下我这个sql建的有什么问题吗?找了两天了,一直抱同一个错误。。

蜡笔小溪 发布于 2013/03/26 11:41
阅读 308
收藏 0
package com.jidisec.log.diarly;

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

public class BooksDB extends SQLiteOpenHelper {
    private final static String DATABASE_NAME = "TEST_DIARYS.db";
    private final static int DATABASE_VERSION = 1;
    private final static String TABLE_NAME = "DIARYS_table";
    public final static String DIARY_ID = "diary_id";
    public final static String DIARY_NAME = "diary_name";
    public final static String DIARY_AUTHOR = "diary_author";
    public final static String DIARY_CUSTOMER = "diary_customer";

    public BooksDB(Context context) {
        // TODO Auto-generated constructor stub
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // 创建table
    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE " + TABLE_NAME + " (" + DIARY_ID
                + " INTEGER primary key autoincrement, " + DIARY_NAME
                + " text, " + DIARY_CUSTOMER + "text," + DIARY_AUTHOR
                + " text);";
        db.execSQL(sql);
        System.out.println("静如见表");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
        db.execSQL(sql);
        onCreate(db);
        System.out.println("静如更新");
    }

    public Cursor select() {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db
                .query(TABLE_NAME, null, null, null, null, null, null);
        System.out.println("静如查询");
        return cursor;
    }

    // 增加操作
    public long insert(String bookname,String cutomer, String author) {
        SQLiteDatabase db = this.getWritableDatabase();
        /* ContentValues */
        ContentValues cv = new ContentValues();
        cv.put(DIARY_NAME, bookname);
        cv.put(DIARY_CUSTOMER, cutomer);
        cv.put(DIARY_AUTHOR, author);
        long row = db.insert(TABLE_NAME, null, cv);
        return row;
    }

    // 删除操作
    public void delete(int id) {
        SQLiteDatabase db = this.getWritableDatabase();
        String where = DIARY_ID + " = ?";
        String[] whereValue = { Integer.toString(id) };
        db.delete(TABLE_NAME, where, whereValue);
    }

    // 修改操作
    public void update(int id, String bookname, String author,String cutomer) {
        SQLiteDatabase db = this.getWritableDatabase();
        String where = DIARY_ID + " = ?";
        String[] whereValue = { Integer.toString(id) };

        ContentValues cv = new ContentValues();
        cv.put(DIARY_NAME, bookname);
        cv.put(DIARY_AUTHOR, author);
        cv.put(DIARY_CUSTOMER, cutomer);
        db.update(TABLE_NAME, cv, where, whereValue);
    }

}


最后我就调用了下这个insert方法,一直抱这个错误

加载中
0
Sassoon
Sassoon

 SQLiteDatabase db = this.getWritableDatabase(); 

感觉是this的原因,一般是用BooksDB 的对象调用,同时要有db的判断是否已打开,最后db关闭,这个代码都写一起了,架构不清晰啊

0
机械师9
机械师9
DIARY_CUSTOMER + "text,"
0
葬泪
葬泪
创建SQLite表是,表中必须有例如"_id integer primary key autoincrement"的字段,而且字段名必须是_id
返回顶部
顶部