2014-06-29 48 views
0

我在數據庫裏有兩個表,但是有一些問題出現。表TBL_PRASEJARAH不存在......這是我的數據庫,幫我解決這個如何在SQLite中修復多個表

package com.beta.manusiapurba; 

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; 
import android.util.Log; 

public class DataGalleryPurba { 
static final String KEY_ROWID = "_id"; 
static final String KEY_NAMA = "nama"; 
static final String KEY_GAMBAR = "gambar"; 
static final String TAG = "DBAdapter"; 

static final String DB_GAMBAR = "db_gambar"; 
static final String TBL_PURBA = "tbl_purba"; 
static final String TBL_PRASEJARAH = "tbl_prasejarah"; 
static final int DB_VERSION = 1; 

static final String CREATE_TBL_PURBA = 
"create table tbl_purba (_id integer primary key autoincrement," + 
"nama text not null, gambar blob not null);"; 

我已經鑄成表tbl_prasejarah但在調試其不存在

static final String CREATE_TBL_PRASEJARAH = 
"create table tbl_prasejarah (_id integer primary key autoincrement," + 
"nama text not null, gambar blob not null);"; 
final Context context; 

DatabaseHelper DBHelper; 
SQLiteDatabase db,db1; 

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

private static class DatabaseHelper extends SQLiteOpenHelper 
{ 

public DatabaseHelper(Context context) { 
super(context, DB_GAMBAR, null, DB_VERSION); 
} 

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

private void loadData(SQLiteDatabase db) { 
inserData("Homo Sapiens", R.drawable.homosatu, db); 
inserData("Homo Erectus", R.drawable.homodua, db); 
inserData("Homo Wajakensis", R.drawable.homotiga, db); 
inserData("Homo", R.drawable.homoempat, db); 
inserData("Homo", R.drawable.homolima, db); 
} 

private void inserData(String manusiapurba, int img, SQLiteDatabase db) { 
ContentValues values = new ContentValues(); 
values.put(KEY_NAMA, manusiapurba); 
values.put(KEY_GAMBAR, img); 
db.insert(TBL_PURBA, null, values); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
Log.w(TAG, "Upgrading database from version "+ oldVersion + " to " 
+ newVersion + ", which will destroy all old data"); 
db.execSQL("DROP TABLE IF EXISTS contacts"); 
onCreate(db); 
} 
} 

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



public Cursor getAllGambarPurba(){ 
Cursor c = db.query(TBL_PURBA, new String[] {KEY_ROWID, KEY_NAMA, KEY_GAMBAR},null,null,null,null,null); 
return c; 
} 

//batas tabel 
public void onCreate(SQLiteDatabase db1) { 
try{ 
db1.execSQL(CREATE_TBL_PRASEJARAH); 
loadData(db1); 
}catch (SQLException e1) { 
e1.printStackTrace(); 
} 
} 

private void loadData(SQLiteDatabase db1) { 
inserData("Homo kwe", R.drawable.homosatu, db1); 
inserData("Homo", R.drawable.homodua, db1); 
inserData("Homo", R.drawable.homotiga, db1); 
inserData("Homo", R.drawable.homoempat, db1); 
inserData("Homo", R.drawable.homolima, db1); 
} 

private void inserData(String zamanprasejarah, int img, SQLiteDatabase db1) { 
ContentValues values = new ContentValues(); 
values.put(KEY_NAMA, zamanprasejarah); 
values.put(KEY_GAMBAR, img); 
db1.insert(TBL_PRASEJARAH, null, values); 
} 

public void onUpgrade(SQLiteDatabase db1, int oldVersion, int newVersion) { 
Log.w(TAG, "Upgrading database from version "+ oldVersion + " to " 
+ newVersion + ", which will destroy all old data"); 
db1.execSQL("DROP TABLE IF EXISTS contacts"); 
onCreate(db1); 
} 


public DataGalleryPurba open1() throws SQLException{ 
db1 = DBHelper.getWritableDatabase(); 
return this; 
} 

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

public Cursor getAllGambarPrasejarah(){ 
Cursor c1 = db1.query(TBL_PRASEJARAH, new String[] {KEY_ROWID, KEY_NAMA, KEY_GAMBAR},null,null,null,null,null); 
return c1; 
} 
} 

回答

0

onCreate()創建的表不在輔助類中,它從不被調用。將代碼移至幫助程序onCreate()並卸載您的應用程序,以便重新創建數據庫。