我尝试在Android Studio中使用现有的SQLite数据库。它可以在Android 10+中工作,但在Android 9及以下版本中,我会收到此错误:
无此表DatabaseHelper
类:
package com.example.testlocaldatabase;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Build;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class DatabaseHelper extends SQLiteOpenHelper {
private static String DB_NAME = "myDatabase.SqLite";
private static String DB_PATH = "";
private static int DB_VERSION = 1;
private SQLiteDatabase database;
private Context context;
private boolean needUpdate = false;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
if (Build.VERSION.SDK_INT >= 17) {
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
} else {
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
}
this.context = context;
copyDatabase();
this.getReadableDatabase();
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
if (i1 > i) {
needUpdate = true;
}
}
private void copyDatabase() {
if (!checkDatabase()) {
this.getReadableDatabase();
this.close();
try {
copyDBFile();
} catch (IOException e) {
throw new Error("Error copying database");
}//catch
}//if
}//copyDatabase
private boolean checkDatabase() {
File dbFile = new File(DB_PATH + DB_NAME);
if (dbFile.exists()) {
return true;
} else {
return false;
}
}
private void copyDBFile() throws IOException {
InputStream inputStream = context.getAssets().open(DB_NAME);
OutputStream outputStream = new FileOutputStream(DB_PATH + DB_NAME);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
outputStream.flush();
outputStream.close();
inputStream.close();
}
public void updateDatabase() throws IOException {
if (needUpdate) {
File dbFile = new File(DB_PATH + DB_NAME);
if (dbFile.exists())
dbFile.delete();
copyDatabase();
needUpdate = false;
}//if
}//updateDatabase
@Override
public synchronized void close() {
if (database != null) {
database.close();
}
super.close();
}
}
字符串MainActivity
:
package com.example.testlocaldatabase;
import androidx.appcompat.app.AppCompatActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private DatabaseHelper databaseHelper;
private SQLiteDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseHelper = new DatabaseHelper(MainActivity.this);
try {
databaseHelper.updateDatabase();
} catch (IOException e) {
e.printStackTrace();
}
database = databaseHelper.getReadableDatabase();
// pull out data from database
Cursor cursor = database.rawQuery("select * from drugs", null);
while (cursor.moveToNext()) {
String b = cursor.getString(cursor.getColumnIndexOrThrow("drugName"));
Log.i("LOG", "" + b);
}
cursor.close();
}
}
型
logcat中的错误行:
android.database.sqlite.SQLiteException:无此表:药物(代码1 SQLITE_ERROR):,编译时:从药物中选择 *
1条答案
按热度按时间laik7k3q1#
在android 13每次你需要更新数据库版本时,你已经做了任何更改数据库模式。
字符串