Phonegap原生插件与sqlite数据库通信

时间:2012-09-13 05:13:57

标签: android sqlite cordova

我将商店数据的java插件写入SQLITE数据库,但此连接URL显示错误

是否需要任何权限?

package com.tricedesigns;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import org.apache.cordova.CordovaWebViewClient;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.sqlite.SQLite;

import com.sun.java.swing.plaf.windows.resources.windows;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
import android.view.Window;



public class HelloPlugin extends Plugin { 

 public static final String NATIVE_ACTION_STRING="nativeAction"; 
  public static final String SUCCESS_PARAMETER="laxman"; 
  SQLiteDatabase myDb = null;
  @Override
  public PluginResult execute(String action, JSONArray data, String callbackId) { 
         Log.d("HelloPlugin", "Hello, this is a native function called from PhoneGap/Cordova!"); 
 //only perform the action if it is the one that should be invoked 
  if (NATIVE_ACTION_STRING.equals(action)) { 
   String resultType = null; 
   try { 
    resultType = data.getString(0); 
    Connection connection = null;  
      ResultSet resultSet = null;  
       Statement statement = null;  

         try {  

            Class.forName("org.sqlite.JDBC");  
               //   webView.loadUrl("jdbc:sqlite:tempdb.db");
               connection = DriverManager  
                             .getConnection("jdbc:sqlite:tempdb.db");  



                      statement = connection.createStatement();  
                         System.out.println("dddddddddddddddddddddd");
                         resultSet = statement  
                                 .executeQuery("SELECT name FROM temp");  
                         while (resultSet.next()) {  
                             System.out.println(" NAME:"  
                                     + resultSet.getString("name"));  
                         }  
                     } catch (Exception e) {  
                         e.printStackTrace();  
                     } finally {  
                         try {  
                             resultSet.close();  
                             statement.close();  
                             connection.close();  
                         } catch (Exception e) {  
                             e.printStackTrace();  
                         }  
                     }  

                     System.out.println("result typeeeeeeeee"+data.getString(0));
               } 
               catch (Exception ex) { 
                     Log.d("HelloPlugin", ex.toString()); 
               } 
               if (resultType.equals(SUCCESS_PARAMETER)) { 
                     try {
                        return new PluginResult(PluginResult.Status.OK, "Yay, Success,First Plugin for android!!!"+data.getString(0));
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
               } 
               else { 
                     return new PluginResult(PluginResult.Status.ERROR, "Oops, Error :("); 
               } 
         } 
         return null; 
  } 

} `

我收到了错误

 09-13 09:53:44.387: W/System.err(330): java.sql.SQLException: opening db: 'tempdb.db': Read-only file system

2 个答案:

答案 0 :(得分:0)

点击此处查看Phonegap插件

https://github.com/phonegap/phonegap-plugins

答案 1 :(得分:0)

从phonegap.js中的getDatabase调用本机方法