Android Sqlite显示没有这样的表

时间:2015-06-26 03:31:55

标签: java android sqlite

我想在数据库中添加一些图像uri。我的数据库表有两列id和String Uri。问题是它显示在尝试将一些Uris插入表时不存在这样的表。这是我的数据库适配器类代码。

package com.example.mystorage;

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

public class DBAdapter {
// for customer registration
static final String KEY_ID = "id";
static final String KEY_URI = "uri";


static final String DATABASE_NAME = "IMAGE_DB";

static final String DATABASE_TABLE = "temp_images1";

static final int DATABASE_VERSION = 2;

static final String DATABASE_CREATE = "create table temp_images1 (id integer       autoincrement, "
        + "uri text not null);";

final Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;

public DBAdapter(Context ctx) {
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper {
    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }

    public void onCreate(SQLiteDatabase db) {
        try {
            db.execSQL(DATABASE_CREATE);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

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

}

public DBAdapter open() throws SQLException {
    db = DBHelper.getWritableDatabase();
    return this;
}

public void close() {
    DBHelper.close();
}



// //////////////////////////////////////for
// customerRegistration////////////////
// customer registration for retrieve data
public Cursor getAllImages() {
    return db.query(DATABASE_TABLE, new String[] {KEY_URI}, null,
            null, null, null, null);
}
public Cursor getContentimage(long id) throws SQLException {
    Cursor c = db.query(true, DATABASE_TABLE,
            new String[] { KEY_ID, KEY_URI },
            KEY_ID + "=" + id, null, null,
            null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}
// customer registration for update data
public boolean updateimages(long id, String uri) {
    ContentValues args = new ContentValues();
    // args.put(KEY_ID,id);
    args.put(KEY_URI, uri);
    return db.update(DATABASE_TABLE, args, KEY_ID + "=" + id, null) > 0;
}

// customer registration for insert data
public long insertImages(String uri) {
    ContentValues args = new ContentValues();
    //args.put(KEY_ID, id);
    args.put(KEY_URI, uri);
    long n = db.insert(DATABASE_TABLE, null, args);
    //db.insertOrThrow(DATABASE_TABLE, null, args);
    return n;
}

// customer registration for remove data
public boolean deleteImages(long id) {
    return db.delete(DATABASE_TABLE, KEY_ID + "=" + id, null) > 0;

}
}

这是My ImageAdapter类,我在这里调用insert方法。 mThumbs是uri Arraylist在检索时存储数据库的内容。

public ImageAdapter(Context c, android.net.Uri uri) {
    mContext = c;
    db= new DBAdapter(mContext);
    try {
    db.open();
    db.insertImages(uri.toString());
    db.close();
    }catch(Exception e){
        e.printStackTrace();
    }
    upadteAllImages();

       notifyDataSetChanged();
}

public ImageAdapter(Context c, ArrayList<Uri> imageUris) {
    mContext = c;
    db= new DBAdapter(mContext);
    try {
    db.open();
    for (int i = 0; i < imageUris.size(); i++){
    db.insertImages(imageUris.get(i).toString());
    }
    db.close();
    }catch(Exception e){
        e.printStackTrace();
    }
    upadteAllImages();

       notifyDataSetChanged();
}

private void upadteAllImages() {
    mTHumbs.clear();

    try{
        db.open();
        Cursor c = db.getAllImages();
        if (c.moveToFirst()) {
            while (c.moveToNext()){
                String uri = c.getString(1);
                mTHumbs.add(Uri.parse(uri));
            }
        }
        //mTHumbs.add((Uri) db.getAllImages());

        db.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}

3 个答案:

答案 0 :(得分:1)

String query = "CREATE TABLE " + DATABASE_TABLE  + "("
            + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + KEY_URI + " TEXT not null "+)";

答案 1 :(得分:0)

在数据库中创建表需要进行一些小的更改。

请看这个 -

[project_dir/tmp/cache/assets"]

答案 2 :(得分:0)

  1. 修复CREATE TABLE中的语法错误。 AUTOINCREMENT只能与INTEGER PRIMARY KEY一起使用,并且您错过了PRIMARY KEY

  2. 删除onCreate()中的try-catch,以便在出现语法问题时出现异常。

  3. 卸载您的应用,以便删除旧数据库,并使用修复的SQL再次运行帮助程序onCreate()