无法打开数据库-Android

前端之家收集整理的这篇文章主要介绍了无法打开数据库-Android前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在用sql开发一个简单的Android应用程序.
我按照以下指南 –
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

尝试打开数据库时出错.
这是我的DataBaseHelp.Java类 –

  1. public class DataBaseHelper extends sqliteOpenHelper{
  2. private static String DB_PATH = "/data/data/and.testDB/databases/";
  3. private static String DB_NAME = "MyData";
  4. private sqliteDatabase myDataBase;
  5. private final Context myContext;
  6. public DataBaseHelper(Context context) {
  7. super(context,DB_NAME,null,1);
  8. this.myContext = context;
  9. }
  10. public void createDataBase() throws IOException{
  11. boolean dbExist = checkDataBase();
  12. if(dbExist){
  13. //do nothing - database already exist
  14. }else{
  15. this.getReadableDatabase();
  16. try {
  17. copyDataBase();
  18. } catch (IOException e) {
  19. throw new Error("Error copying database");
  20. }
  21. }
  22. }
  23. private boolean checkDataBase(){
  24. sqliteDatabase checkDB = null;
  25. try{
  26. String myPath = DB_PATH + DB_NAME;
  27. checkDB = sqliteDatabase.openDatabase(myPath,sqliteDatabase.OPEN_READONLY);
  28. }catch(sqliteException e){
  29. //database does't exist yet.
  30. }
  31. if(checkDB != null){
  32. checkDB.close();
  33. }
  34. return checkDB != null ? true : false;
  35. }
  36. private void copyDataBase() throws IOException{
  37. //Open your local db as the input stream
  38. InputStream myInput = myContext.getAssets().open(DB_NAME);
  39. // Path to the just created empty db
  40. String outFileName = DB_PATH + DB_NAME;
  41. //Open the empty db as the output stream
  42. OutputStream myOutput = new FileOutputStream(outFileName);
  43. //transfer bytes from the inputfile to the outputfile
  44. byte[] buffer = new byte[1024];
  45. int length;
  46. while ((length = myInput.read(buffer))>0){
  47. myOutput.write(buffer,length);
  48. }
  49. //Close the streams
  50. myOutput.flush();
  51. myOutput.close();
  52. myInput.close();
  53. }
  54. public void openDataBase() {
  55. //Open the database
  56. String myPath = DB_PATH + DB_NAME;
  57. myDataBase = sqliteDatabase.openDatabase(myPath,sqliteDatabase.OPEN_READONLY);
  58. }
  59. @Override
  60. public synchronized void close() {
  61. if(myDataBase != null)
  62. myDataBase.close();
  63. super.close();
  64. }
  65. @Override
  66. public void onCreate(sqliteDatabase db) {
  67. }
  68. @Override
  69. public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
  70. }
  71. }

这是我的testDB.Java类 –

  1. public class testDB extends Activity {
  2. sqliteDatabase myDataBase;
  3. public String[] gur = new String[4];
  4. /** Called when the activity is first created. */
  5. @Override
  6. public void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.main);
  9. DataBaseHelper myDbHelper = new DataBaseHelper(this);
  10. myDbHelper = new DataBaseHelper(this);
  11. try {
  12. myDbHelper.createDataBase();
  13. } catch (IOException ioe) {
  14. throw new Error("Unable to create database");
  15. }
  16. try {
  17. myDbHelper.openDataBase();
  18. // Here is the problem-Can't open the database/
  19. }catch(sqlException sqle){
  20. throw sqle;
  21. }
  22. myDataBase = myDbHelper.getWritableDatabase();
  23. Cursor c = myDataBase.rawQuery("SELECT * FROM beer",null);
  24. if (c != null ) {
  25. if (c.moveToFirst()) {
  26. for(int x=0;x< 1;x++)
  27. {
  28. gur[x] = (c.getString(c.getColumnIndex("name")));
  29. c.moveToNext();
  30. }
  31. }
  32. Toast.makeText(this,gur[0],Toast.LENGTH_LONG).show();
  33. }
  34. }
  35. }

我使用sqlite数据库浏览器创建了数据库,创建了一个名为“beer”的表,添加了2行并将其放在我项目的/ assests文件夹中.

请注意,我是sql的新手,也是我第一次使用它.我整天都在寻找答案而找不到答案.

谢谢!

最佳答案
sqlite数据库浏览器中打开数据库添加一个名为“android_Metadata”的新表,

您可以执行以下sql语句来执行此操作:

CREATE TABLE“android_Metadata”(“locale”TEXT DEFAULT’en_US’)

现在在“android_Metadata”表中插入一行文本“en_US”:

INSERT INTO“android_Metadata”VALUES(‘en_US’)

然后,有必要将表的主id字段重命名为“_id”,以便Android知道绑定表的id字段的位置.

现在将新数据库文件复制到您的项目资产文件夹中,它将工作…..

猜你在找的Android相关文章