PhoneGap(3.4.0)与sqlite的Android应用程序,它在模拟器中工作正常但不在设备中

时间:2014-10-29 11:32:40

标签: android cordova

我已经在带有sqlite的PhoneGap(3.4.0)android应用程序中创建了它,它在模拟器 中运行良好,但在设备中无法正常工作 。我正在使用包含大量记录的填充数据库。我google了很多,但没有解决我的问题。 错误

SQLitePlugin.executeSql[Batch](): Error=no such table: my_table_name (code 1): , while compiling: SELECT * from my_table_name

我的代码:

db = window.sqlitePlugin.openDatabase("myDB.sqlite", "1.0", "DB", 4000000);
db.transaction(function(tx) {      
     tx.executeSql("SELECT * from my_table_name", [], function(tx, res)  {                                           
      });
});

1 个答案:

答案 0 :(得分:0)

我用这个解决了我的问题,如果你正在为模拟器工作,那么使用命令提示符复制你的数据库,我在资产文件夹中添加了myDB.sqlite然后运行

adb push myDB.sqlite /data/data/com.my_app.my_app/databases/myDB.sqlite

对于设备,只需将它们放入到Android的应用程序包中即资产文件夹。你需要myDB.sqlite和file__0 / 0000000000000001.sqlite文件。的 More....

public void onCreate(Bundle savedInstanceState)
{
   super.onCreate(savedInstanceState);
   super.init();
   try
   {
        String pName = this.getClass().getPackage().getName();
        this.copy("myDB.sqlite","/data/data/"+pName+"/databases/");
        this.copy("0000000000000001.sqlite","/data/data/"+pName+"/app_database/file__0/");
   }
   catch (IOException e)
   {
       e.printStackTrace();
   }

  super.loadUrl(Config.getStartUrl());    
}

void copy(String file, String folder) throws IOException 
{

   File CheckDirectory;
   CheckDirectory = new File(folder);
   if (!CheckDirectory.exists())
   { 
     CheckDirectory.mkdir();
   }

   InputStream in = getApplicationContext().getAssets().open(file);
   OutputStream out = new FileOutputStream(folder+file);
   // Transfer bytes from in to out
   byte[] buf = new byte[1024];
   int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len);
   in.close(); out.close();

}

对于iOS,您可以使用以下步骤:

第1步: sqlite 数据库中删除 .sqlite 扩展程序。防爆。如果我们有一个名为 myDB.sqlite 的数据库,只能将其设为 myDB

第2步:然后选择“为任何添加的文件夹创建群组”选项,将 myDB 文件拖到Xcode中的资源目录中,然后选中“将项目复制到目标位置”组的文件夹(如果需要)“添加引用时的选项。

第3步:现在使用以下代码将数据库复制到应用程序内的Document目录中。

-(void) copyDatabase{

   NSFileManager *fileManager = [NSFileManager defaultManager];
   NSError *error;

   NSString *dbPath = [self getDBPath];
   BOOL success = [fileManager fileExistsAtPath:dbPath];

   if(!success) {

     NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"myDB"];
     success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

     if (!success)
       NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
  }
 }

- (NSString *) getDBPath
{

   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
   NSString *documentsDir = [paths objectAtIndex:0];
   return [documentsDir stringByAppendingPathComponent:@"myDB"];
}

第4步:使用此代码 [self copyDatabase]; 并在 didFinishLaunchingWithOptions(AppDelegate.m)方法中调用此代码,如下所示< / p>

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
   [self copyDatabase];
}

现在使用以下代码打开数据库。

function openDatabase() {
      try{
           if(!!window.openDatabase){
            var openDB = window.sqlitePlugin.openDatabase({name : "myDB"});
           }else{
              navigator.notification.alert("Local database is not supported by your device.");
           }
      }
      catch(error){
         console.log("ERROR:" + error.message);
 }

<强> More detail...