我已经尝试了很多时间,但是无法得到如何解决这个问题,SQL Broswser上的一切正常,SQL查询运行没有得到但是在运行问题时,而不是获取
Caused by: android.database.sqlite.SQLiteException: no such table
。请告诉我,这是怎么回事。
public class DatabaseHelper extends SQLiteOpenHelper {
private Context mycontext;
private String DB_PATH = "/data/data/com.tosts.yummi/databases/";
private static String DB_NAME = "hotss";
private static String DB_TABLE = "kathas";
public SQLiteDatabase myDataBase;
private int id;
private static final int DATABASE_VERSION = 3;
private String TAG = "horror";
public DatabaseHelper(Context context) throws IOException {
super(context, DB_NAME, null, DATABASE_VERSION);
this.mycontext = context;
boolean dbexist = checkdatabase();
if (dbexist) {
// System.out.println("Database exists");
opendatabase();
} else {
// System.out.println("Database doesn't exist");
createdatabase();
}
}
public void createdatabase() throws IOException {
boolean dbexist = checkdatabase();
if (dbexist) {
System.out.println(" Database exists.");
} else {
this.getReadableDatabase();
try {
copydatabase();
//myDataBase.close();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkdatabase() {
// SQLiteDatabase checkdb = null;
boolean checkdb = false;
try {
String myPath = DB_PATH + DB_NAME;
File dbfile = new File(myPath);
checkdb = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE) != null;
checkdb = dbfile.exists();
} catch (SQLiteException e) {
System.out.println("Database doesn't exist");
}
return checkdb;
}
private void copydatabase() throws IOException {
// Open your local db as the input stream
InputStream myinput = mycontext.getAssets().open(DB_NAME);
String outfilename = DB_PATH + DB_NAME;
OutputStream myoutput = new FileOutputStream(outfilename);
Log.d(TAG, "COpt Test->"+"InputStream->"+myinput+"--?"+outfilename+"---"+myoutput);
byte[] buffer = new byte[1024];// transfer byte to inputfile to
// outputfile
int length;
while ((length = myinput.read(buffer)) > 0) {
myoutput.write(buffer, 0, length);
}
myoutput.flush();// Close the streams
myoutput.close();
myinput.close();
}
public void opendatabase() throws SQLException {
// Open the database
String mypath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(mypath, null,
SQLiteDatabase.OPEN_READONLY);
}
public synchronized void close() {
if (myDataBase != null) {
myDataBase.close();
}
super.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
//onCreate(db);
}
// This will return a cursor containing database records
public Cursor data() {
Cursor c;
c = myDataBase.query(DB_TABLE, null, null, null, null, null, null);
return c;
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}
public List<String> getAllKatha(int category){
List<String> labels = new ArrayList<String>();
//List<String> ids = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT * FROM kathas where category =" + category;
Log.d(TAG, "--->>>"+selectQuery);
myDataBase = this.getReadableDatabase();
Cursor cursor = myDataBase.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(1));
//ids.add(cursor.getString(0));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
myDataBase.close();
// returning lables
return labels;
}
我在log cat上得到这样的结果:
12-23 19:37:51.503: E/AndroidRuntime(1527): Caused by: android.database.sqlite.SQLiteException: no such table: kathas (code 1): , while compiling: SELECT * FROM kathas where category =3
12-23 19:37:51.503: E/AndroidRuntime(1527): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
12-23 19:37:51.503: E/AndroidRuntime(1527): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
12-23 19:37:51.503: E/AndroidRuntime(1527): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
12-23 19:37:51.503: E/AndroidRuntime(1527): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
12-23 19:37:51.503: E/AndroidRuntime(1527): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
12-23 19:37:51.503: E/AndroidRuntime(1527): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
12-23 19:37:51.503: E/AndroidRuntime(1527): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
12-23 19:37:51.503: E/AndroidRuntime(1527): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
12-23 19:37:51.503: E/AndroidRuntime(1527): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
12-23 19:37:51.503: E/AndroidRuntime(1527): at com.nepal.nepalisexstories.database.DatabaseHelper.getAllKatha(DatabaseHelper.java:139)
12-23 19:37:51.503: E/AndroidRuntime(1527): at com.nepal.nepalisexstories.DisplayTitle.onCreate(DisplayTitle.java:70)
实际上是在MainActivity.java上执行此操作
titles = db.getAllKatha(category); ///(DisplayTitle.java:70)
先谢谢。
答案 0 :(得分:0)
错误是显式安静的,表“kathas”尚未创建,这是因为您在SQLiteOpenHelper的“onCreate”方法中没有任何内容,并且在您用于创建数据库的方法中出现了问题,这是一个关于数据库创建的糟糕实现,你应该使用在SQLiteOpenHelper的onCreate方法中传递的SQLiteDatabase参数,我也注意到你正在使用onUpgrade方法调用没有任何东西的“onCreate”,所以如果您更新数据库,实际上没有发生任何事情。
答案 1 :(得分:0)
您的onCreate
方法中没有任何内容,但这并不是那么糟糕,因为您正在复制现有数据库。真正糟糕的是,你正在执行的onUpgrade
方法:
DROP TABLE IF EXISTS kathas
然后你打电话给你的onCreate
什么都不做。
显然,找不到你的表 - 因为你明确地删除了它。