“找不到表” 错误在 Android 9 及以下版本中使用现有数据库时发生。

huangapple go评论109阅读模式
英文:

"No such table" Error using an existing database in Android 9 and below

问题

我正在尝试在Android Studio中使用现有的SQLite数据库。在Android 10+中可以正常工作,但在Android 9及以下版本中出现以下错误:

没有这个表

DatabaseHelper 类:

  1. package com.example.testlocaldatabase;
  2. import android.content.Context;
  3. import android.database.sqlite.SQLiteDatabase;
  4. import android.database.sqlite.SQLiteOpenHelper;
  5. import android.os.Build;
  6. import java.io.File;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.OutputStream;
  11. public class DatabaseHelper extends SQLiteOpenHelper {
  12. private static String DB_NAME = "myDatabase.SqLite";
  13. private static String DB_PATH = "";
  14. private static int DB_VERSION = 1;
  15. private SQLiteDatabase database;
  16. private Context context;
  17. private boolean needUpdate = false;
  18. public DatabaseHelper(Context context) {
  19. super(context, DB_NAME, null, DB_VERSION);
  20. if (Build.VERSION.SDK_INT >= 17) {
  21. DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
  22. } else {
  23. DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
  24. }
  25. this.context = context;
  26. copyDatabase();
  27. this.getReadableDatabase();
  28. }
  29. @Override
  30. public void onCreate(SQLiteDatabase sqLiteDatabase) {
  31. }
  32. @Override
  33. public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
  34. if (i1 > i) {
  35. needUpdate = true;
  36. }
  37. }
  38. private void copyDatabase() {
  39. if (!checkDatabase()) {
  40. this.getReadableDatabase();
  41. this.close();
  42. try {
  43. copyDBFile();
  44. } catch (IOException e) {
  45. throw new Error("Error copying database");
  46. }//catch
  47. }//if
  48. }//copyDatabase
  49. private boolean checkDatabase() {
  50. File dbFile = new File(DB_PATH + DB_NAME);
  51. if (dbFile.exists()) {
  52. return true;
  53. } else {
  54. return false;
  55. }
  56. }
  57. private void copyDBFile() throws IOException {
  58. InputStream inputStream = context.getAssets().open(DB_NAME);
  59. OutputStream outputStream = new FileOutputStream(DB_PATH + DB_NAME);
  60. byte[] buffer = new byte[1024];
  61. int length;
  62. while ((length = inputStream.read(buffer)) > 0) {
  63. outputStream.write(buffer, 0, length);
  64. }
  65. outputStream.flush();
  66. outputStream.close();
  67. inputStream.close();
  68. }
  69. public void updateDatabase() throws IOException {
  70. if (needUpdate) {
  71. File dbFile = new File(DB_PATH + DB_NAME);
  72. if (dbFile.exists())
  73. dbFile.delete();
  74. copyDatabase();
  75. needUpdate = false;
  76. }//if
  77. }//updateDatabase
  78. @Override
  79. public synchronized void close() {
  80. if (database != null) {
  81. database.close();
  82. }
  83. super.close();
  84. }
  85. }

MainActivity:

  1. package com.example.testlocaldatabase;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import java.io.IOException;
  8. public class MainActivity extends AppCompatActivity {
  9. private DatabaseHelper databaseHelper;
  10. private SQLiteDatabase database;
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. databaseHelper = new DatabaseHelper(MainActivity.this);
  16. try {
  17. databaseHelper.updateDatabase();
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. database = databaseHelper.getReadableDatabase();
  22. // 从数据库中提取数据
  23. Cursor cursor = database.rawQuery("select * from drugs", null);
  24. while (cursor.moveToNext()) {
  25. String b = cursor.getString(cursor.getColumnIndexOrThrow("drugName"));
  26. Log.i("LOG", "" + b);
  27. }
  28. cursor.close();
  29. }
  30. }

在日志中的错误行:

android.database.sqlite.SQLiteException: no such table: drugs (code 1 SQLITE_ERROR): , while compiling: select * from drugs

英文:

I'm trying to use an existing SQLite database in Android Studio. It works in Android 10+ but in Android 9 and below I get this error:

> No such table

DatabaseHelper class:

  1. package com.example.testlocaldatabase;
  2. import android.content.Context;
  3. import android.database.sqlite.SQLiteDatabase;
  4. import android.database.sqlite.SQLiteOpenHelper;
  5. import android.os.Build;
  6. import java.io.File;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.OutputStream;
  11. public class DatabaseHelper extends SQLiteOpenHelper {
  12. private static String DB_NAME = "myDatabase.SqLite";
  13. private static String DB_PATH = "";
  14. private static int DB_VERSION = 1;
  15. private SQLiteDatabase database;
  16. private Context context;
  17. private boolean needUpdate = false;
  18. public DatabaseHelper(Context context) {
  19. super(context, DB_NAME, null, DB_VERSION);
  20. if (Build.VERSION.SDK_INT >= 17) {
  21. DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
  22. } else {
  23. DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
  24. }
  25. this.context = context;
  26. copyDatabase();
  27. this.getReadableDatabase();
  28. }
  29. @Override
  30. public void onCreate(SQLiteDatabase sqLiteDatabase) {
  31. }
  32. @Override
  33. public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
  34. if (i1 > i) {
  35. needUpdate = true;
  36. }
  37. }
  38. private void copyDatabase() {
  39. if (!checkDatabase()) {
  40. this.getReadableDatabase();
  41. this.close();
  42. try {
  43. copyDBFile();
  44. } catch (IOException e) {
  45. throw new Error("Error copying database");
  46. }//catch
  47. }//if
  48. }//copyDatabase
  49. private boolean checkDatabase() {
  50. File dbFile = new File(DB_PATH + DB_NAME);
  51. if (dbFile.exists()) {
  52. return true;
  53. } else {
  54. return false;
  55. }
  56. }
  57. private void copyDBFile() throws IOException {
  58. InputStream inputStream = context.getAssets().open(DB_NAME);
  59. OutputStream outputStream = new FileOutputStream(DB_PATH + DB_NAME);
  60. byte[] buffer = new byte[1024];
  61. int length;
  62. while ((length = inputStream.read(buffer)) > 0) {
  63. outputStream.write(buffer, 0, length);
  64. }
  65. outputStream.flush();
  66. outputStream.close();
  67. inputStream.close();
  68. }
  69. public void updateDatabase() throws IOException {
  70. if (needUpdate) {
  71. File dbFile = new File(DB_PATH + DB_NAME);
  72. if (dbFile.exists())
  73. dbFile.delete();
  74. copyDatabase();
  75. needUpdate = false;
  76. }//if
  77. }//updateDatabase
  78. @Override
  79. public synchronized void close() {
  80. if (database != null) {
  81. database.close();
  82. }
  83. super.close();
  84. }
  85. }

MainActivity :

  1. package com.example.testlocaldatabase;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import java.io.IOException;
  8. public class MainActivity extends AppCompatActivity {
  9. private DatabaseHelper databaseHelper;
  10. private SQLiteDatabase database;
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. databaseHelper = new DatabaseHelper(MainActivity.this);
  16. try {
  17. databaseHelper.updateDatabase();
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. database = databaseHelper.getReadableDatabase();
  22. // pull out data from database
  23. Cursor cursor = database.rawQuery("select * from drugs", null);
  24. while (cursor.moveToNext()) {
  25. String b = cursor.getString(cursor.getColumnIndexOrThrow("drugName"));
  26. Log.i("LOG", "" + b);
  27. }
  28. cursor.close();
  29. }
  30. }

Error line in logcat :

> android.database.sqlite.SQLiteException: no such table: drugs (code 1 SQLITE_ERROR): , while compiling: select * from drugs

答案1

得分: 1

在Android 13上,每当您在数据库模式中进行任何更改时,都需要更新数据库版本。

  1. public class DatabaseHelper extends SQLiteOpenHelper {
  2. private static String DB_NAME = "myDatabase.SqLite";
  3. private static String DB_PATH = "";
  4. private static int DB_VERSION = 2;
  5. private SQLiteDatabase database;
  6. private Context context;
  7. private boolean needUpdate = false;
  8. public DatabaseHelper(Context context) {
  9. super(context, DB_NAME, null, DB_VERSION);
  10. if (Build.VERSION.SDK_INT >= 17) {
  11. DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
  12. } else {
  13. DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
  14. }
  15. this.context = context;
  16. copyDatabase();
  17. this.getReadableDatabase();
  18. }
  19. }
英文:

On android 13 every time you need to update the database version when you have done any changes in DB schema.

  1. public class DatabaseHelper extends SQLiteOpenHelper {
  2. private static String DB_NAME = "myDatabase.SqLite";
  3. private static String DB_PATH = "";
  4. private static int DB_VERSION = 2;
  5. private SQLiteDatabase database;
  6. private Context context;
  7. private boolean needUpdate = false;
  8. public DatabaseHelper(Context context) {
  9. super(context, DB_NAME, null, DB_VERSION);
  10. if (Build.VERSION.SDK_INT >= 17) {
  11. DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
  12. } else {
  13. DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
  14. }
  15. this.context = context;
  16. copyDatabase();
  17. this.getReadableDatabase();
  18. }

huangapple
  • 本文由 发表于 2023年7月31日 23:15:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定