PhoneGap(3.4.0) 带有 sqlite 的 android 应用程序,它在模拟器中运行良好,但在设备中运行良好 [英] PhoneGap(3.4.0) android app with sqlite, its working fine in emulator but not in device

查看:20
本文介绍了PhoneGap(3.4.0) 带有 sqlite 的 android 应用程序,它在模拟器中运行良好,但在设备中运行良好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 sqlite 在 PhoneGap(3.4.0) android 应用程序中创建,它在模拟器中运行良好但在设备中无法运行.我正在使用包含大量记录的填充数据库.我用谷歌搜索了很多,但没有解决我的问题.错误是:

I have created in PhoneGap(3.4.0) android app with sqlite, its working fine in emulator but not working in device. I am using populated database with lots of records. I have googled to much, but not solved my issue. Error is:

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)  {                                           
      });
});

推荐答案

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

I solved my issue using this, If you are working for emulator then copy your databas using command prompt, I have added myDB.sqlite in asset folder then run

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

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

For device, just put them in to bundle of app that is assets folder in case of Android . you need myDB.sqlite and file__0/0000000000000001.sqlite files. 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.

For iOS you can use following steps:

Step 1 : Remove the .sqlite extension from the sqlite database. Ex. If we have the database called myDB.sqlite make it only myDB.

第 2 步:然后通过选择为任何添加的文件夹创建组"选项将 myDB 文件拖到 Xcode 中的资源目录并选中将项目复制到目的地组的文件夹(如果需要)"选项同时添加引用.

Step 2 : Then drag the myDB file to Resource directory in Xcode by choosing the option "Create groups for any added folder" and check the "Copy items into destination group’s folder (if needed)" option while adding reference.

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

Step 3 : Now use the following code to copy the database into the Document directory inside our application.

-(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 ) 方法中调用它,如下所示

Step 4 : Use this code [self copyDatabase]; and call this in the didFinishLaunchingWithOptions(AppDelegate.m ) method like this

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

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

Now open the database using following code.

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);
 }

更多细节...

这篇关于PhoneGap(3.4.0) 带有 sqlite 的 android 应用程序,它在模拟器中运行良好,但在设备中运行良好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆