本文介绍了PhoneGap的(3.4.0)Android应用程序使用SQLite,其做工精细的仿真器,但不是在装置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PhoneGap的创建(3.4.0)的Andr​​oid使用SQLite的应用程序,它的做工精细的仿真器的而不是在设备工作。我使用的人口数据库有很多的记录。我用Google搜索到很多,但没有解决我的问题。 错误是

  SQLitePlugin.executeSql [批量]():错误=没有这样的表:MY_TABLE_NAME(code 1):在编制:SELECT * FROM MY_TABLE_NAME
 

我的code:

  DB = window.sqlitePlugin.openDatabase(myDB.sqlite,1.0,DB,400万);
db.transaction(函数(德克萨斯州){
     tx.executeSql(选择从MY_TABLE_NAME *,[],功能(德克萨斯州,RES){
      });
});
 

解决方案

我解决了使用这个我的问题,如果您然后复制使用命令提示符的databas工作的模拟器,我已经加入myDB.sqlite资产的文件夹,然后运行

亚行推动myDB.sqlite /data/data/com.my_app.my_app/databases/myDB.sqlite

有关的设备,只是把他们捆绑的应用程序,在Android的情况下,资产的文件夹。你需要myDB.sqlite和file__0 / 0000000000000001.sqlite文件。 更多....

 公共无效的onCreate(包savedInstanceState)
{
   super.onCreate(savedInstanceState);
   super.init();
   尝试
   {
        字符串PNAME = this.getClass()getPackage()的getName()。
        this.copy(myDB.sqlite,/数据/数据​​/+ PNAME +/数据库/);
        this.copy(0000000000000001.sqlite,/数据/数据​​/+ PNAME +/ app_database / file__0 /);
   }
   赶上(IOException异常E)
   {
       e.printStackTrace();
   }

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

无效副本(字符串文件,字符串文件夹)抛出IOException异常
{

   文件CheckDirectory;
   CheckDirectory =新的文件(文件夹);
   如果(!CheckDirectory.exists())
   {
     CheckDirectory.mkdir();
   }

   InputStream的时间= getApplicationContext()getAssets()打开(文件)。;
   的OutputStream OUT =新的FileOutputStream(文件夹+文件);
   //传输的字节从以出
   byte []的BUF =新的字节[1024];
   INT LEN;而((LEN = in.read(BUF))大于0)out.write(buf中,0,的len);
   附寄(); out.close();

}
 

为iOS,您可以使用以下步骤:

第1步:删除在 .sqlite 扩展从源码数据库。例如果我们都称为数据库的 myDB.sqlite 使其仅 MYDB

第二步:,然后拖动 MYDB 文件通过选择选项创建任何附加文件夹组,并检查资源的X code目录的复制项目到目的地组的文件夹(如果需要)选项,同时增加参考。

第三步:现在使用下面的code中的数据库复制到我们的应用程序内的文件目录

   - (无效)copyDatabase {

   的NSFileManager *文件管理器=的NSFileManager defaultManager]
   NSError *错误;

   的NSString * DBPATH = [自我getDBPath]
   BOOL成功= [文件管理器fileExistsAtPath:DBPATH]。

   如果(!成功){

     的NSString * defaultDBPath = [[[一个NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@mydb的]。
     成功= [文件管理器copyItemAtPath:defaultDBPath toPath:DBPATH错误:放大器;错误]

     如果(!成功)
       NSAssert1(0,@无法创建消息%@可写的数据库文件。[错误localizedDescription]);
  }
 }

 - (的NSString *)getDBPath
{

   的NSArray *路径= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
   的NSString * documentsDir = [路径objectAtIndex:0];
   返回[documentsDir stringByAppendingPathComponent:@mydb的]。
}
 

第四步::使用此code [自copyDatabase] 并调用这个在 didFinishLaunchingWithOptions(AppDelegate.m)方式像这样

   - (BOOL)申请:(的UIApplication *)申请didFinishLaunchingWithOptions:(NSDictionary中*)launchOptions
{
   [自copyDatabase]
}
 

现在使用下面的code打开数据库。

 函数的openDatabase(){
      尝试{
           如果(!window.openDatabase){
            VAR openDB = window.sqlitePlugin.openDatabase({名字:MYDB});
           }其他{
              navigator.notification.alert(本地数据库是不是您的设备支持。);
           }
      }
      赶上(错误){
         的console.log(错误:+返回Error.message);
 }
 

<一个href="http://mindfiremobile.word$p$pss.com/2014/03/12/copy-existing-sqlite-database-into-phonegap-ios-application/"相对=nofollow>更多细节...

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

My code:

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

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

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

}

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.

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.

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"];
}

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

More detail...

这篇关于PhoneGap的(3.4.0)Android应用程序使用SQLite,其做工精细的仿真器,但不是在装置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 14:03