android中的sqlite3_open错误

时间:2012-08-17 06:40:29

标签: java android sqlite

          public class DataBaseHelper extends SQLiteOpenHelper
          {
              private static String DB_NAME;
              private static String DB_PATH = "/data/data/com.muthu.tamil/databases/";
              private final Context myContext;
              private SQLiteDatabase myDataBase;

             static
              {
                  DB_NAME = "aathichudi.db";
              }

  public DataBaseHelper(Context paramContext)
  {
    super(paramContext, DB_NAME, null, 1);
    DB_PATH = "/data/data/" + paramContext.getPackageName() + "/databases/";
    this.myContext = paramContext;
  }

  private boolean checkDataBase()
  {
    return new File(DB_PATH + DB_NAME).exists();
  }

  private void copyDataBase()
    throws IOException
  {
    InputStream localInputStream = this.myContext.getAssets().open(DB_NAME);
    FileOutputStream localFileOutputStream = new FileOutputStream(DB_PATH + DB_NAME);
    byte[] arrayOfByte = new byte[1024];
    while (true)
    {
      int i = localInputStream.read(arrayOfByte);
      if (i <= 0)
      {
        localFileOutputStream.flush();
        localFileOutputStream.close();
        localInputStream.close();
        return;
      }
      localFileOutputStream.write(arrayOfByte, 0, i);
    }
  }




  public void createDataBase()
    throws IOException
  {
    if (checkDataBase())
      new File(DB_PATH + DB_NAME).delete();
    getReadableDatabase();
    close();
    try
    {
      copyDataBase();
      return;
    }
    catch (IOException localIOException)
    {
    }
    throw new Error("Error copying database");
  }

  public Cursor getData(String paramString)
  {
    return this.myDataBase.rawQuery(paramString, null);
  }

  public void onCreate(SQLiteDatabase paramSQLiteDatabase)
  {
  }

  public void onUpgrade(SQLiteDatabase paramSQLiteDatabase, int paramInt1, int paramInt2)
  {
  }

  public void openDataBase()
    throws SQLException
  {
    this.myDataBase = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, 1);
  }

我在android中为db连接编写代码,但是我得到了这样的ERROE:

  

sqlite3_open_v2( “/数据/数据/ com.muthu,泰米尔语/数据库/ aathichudi.db”,   &amp; handle,1,NULL)失败   sqlite3_open_v2(“/ data / data / com.muthu,tamil / databases / aathichudi.db”,&amp; handle,1,NULL)失败    sqlite返回:错误代码= 14,msg =无法在源行25467打开文件    sqlite3_open_v2(“/ data / data // data / data / com.muthu.tamil / files / databases / aathichudi.db”,&amp; handle,1,NULL)失败    关闭VM    threadid = 1:线程退出,未捕获异常(group = 0x4001d800)    致命异议:主要    java.lang.RuntimeException:无法启动活动ComponentInfo {com.muthu.tamil / com.muthu.tamil.HomeActivity}:android.database.sqlite.SQLiteException:无法打开数据库文件    在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)   在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)       在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)       在android.os.Handler.dispatchMessage(Handler.java:99)       在android.os.Looper.loop(Looper.java:123)       在android.app.ActivityThread.main(ActivityThread.java:4627)       at java.lang.reflect.Method.invokeNative(Native Method)       在java.lang.reflect.Method.invoke(Method.java:521)       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)       at dalvik.system.NativeStart.main(Native Method)      引起:android.database.sqlite.SQLiteException:无法打开数据库文件       在android.database.sqlite.SQLiteDatabase.dbopen(本机方法)       在android.database.sqlite.SQLiteDatabase。(SQLiteDatabase.java:1812)       在android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)       在com.muthu.tamil.sql.DataBaseHelper.openDataBase(DataBaseHelper.java:112)       在com.muthu.tamil.HomeActivity.onCreate(HomeActivity.java:41)       在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)       ......还有11个

1 个答案:

答案 0 :(得分:2)

sqlite3_open_v2("/data/data/com.muthu,tamil/databases/aathichudi.db", &handle, 1, NULL)
failed sqlite returned: error code = 14, msg = cannot open file at source line 25467 

仅当您未在模拟器或设备中创建数据库时,才会生成此错误。您可以检查是否在IDE DDMS / file_explorer(/ data / data / Ur_pkg_name / databases / ur_db_name)中创建了数据库。

报告了与此问题相关的错误:http://code.google.com/p/android/issues/detail?id=949

我遇到过几个人提出的解决方案/解决方法,包括以下内容:

如果在Manifest文件中使用sharedUserId,则更改应用程序的sharedUserId并重新安装应用程序可能会有效,因为它没有写入SQLite数据库所需的所有权。

编辑清单文件可以再次消除错误。在清单中,确保您输入了正确的min sdk版本。您可以通过检查清单文件确保您的min SDK版本没问题,如果代码左栏中有一个感叹号指示器告诉我您的错误。

我希望其中一种解决方案也适合你。