如何在java类中打开SQLite DB

时间:2012-11-01 07:22:42

标签: java android database sqlite exception

我从活动(android)调用java类中的db。可以从活动调用这样的java类。用于服务器通信android.Am从服务器获取数据但我无法插入数据库。

我打电话说,如果任何人帮助我,我会得到例外,提前谢谢。

11-01 06:52:03.596: W/System.err(673): java.lang.NullPointerException
11-01 06:52:03.603: W/System.err(673):  at com.commiunication.ServerConnection.Insert(ServerConnection.java:32)
11-01 06:52:03.627: W/System.err(673):  at com.commiunication.MainActivity.serverconnection(MainActivity.java:138)
11-01 06:52:03.627: W/System.err(673):  at com.commiunication.MainActivity.onCreate(MainActivity.java:46)
11-01 06:52:03.633: W/System.err(673):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-01 06:52:03.633: W/System.err(673):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-01 06:52:03.633: W/System.err(673):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-01 06:52:03.633: W/System.err(673):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-01 06:52:03.633: W/System.err(673):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-01 06:52:03.633: W/System.err(673):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 06:52:03.633: W/System.err(673):  at android.os.Looper.loop(Looper.java:123)
11-01 06:52:03.633: W/System.err(673):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-01 06:52:03.633: W/System.err(673):  at java.lang.reflect.Method.invokeNative(Native Method)
11-01 06:52:03.655: W/System.err(673):  at java.lang.reflect.Method.invoke(Method.java:521)
11-01 06:52:03.655: W/System.err(673):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-01 06:52:03.655: W/System.err(673):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-01 06:52:03.655: W/System.err(673):  at dalvik.system.NativeStart.main(Native Method)

androidactivity.java

ServerConnection server=new ServerConnection();
try {

server.Insert(ReturnValue);
sampleDB =    this.openOrCreateDatabase(SAMPLE_DB_NAME,MODE_PRIVATE,null);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

serverConnection.java

public  void Insert(String ReturnValue) throws JSONException{
JSONObject CDGSGJson=new JSONObject(ReturnValue);           
JSONArray idarr = CDGSGJson.getJSONArray("MeasureArr"); 
System.out.println(""+idarr.length());  
sampleDB= openOrCreateDatabase(SAMPLE_DB_NAME,     SQLiteDatabase.OPEN_READWRITE, null);
System.out.println(""+idarr.length());  
//Toast.makeText(this, "Group Details Downloading", Toast.LENGTH_SHORT).show();
try {   
sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " + table_name + " "
+ "( id INTEGER PRIMARY KEY AUTOINCREMENT," + " measure TEXT NOT NULL ); ");       
if(idarr.length()>0){

for(int cc=0;cc<idarr.length();cc++){               
JSONObject CatObj = idarr.getJSONObject(cc);
String id_get=CatObj.getString("id");   
String measure_get=CatObj.getString("measure"); 

sampleDB.execSQL("INSERT OR REPLACE INTO " + table_name+ " Values      
                                                ('"+id_get+"','"+measure_get+"');");

            }}

    } catch (Exception e) {
        // TODO: handle exception
         e.printStackTrace();
        // System.out.print(e.printStackTrace());
    }

2 个答案:

答案 0 :(得分:0)

在android中为了使用db,有一个内置的api叫做SqLiteOpenHelper。

请浏览http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html

以下是示例代码

http://www.vogella.com/articles/AndroidSQLite/article.html

答案 1 :(得分:0)

请参阅以下链接可能对您有所帮助。

对于本地数据库交互:Link1。您可以直接从中下载源代码并尝试理解它。 Link2

对于远程数据库:对于远程数据库,您必须创建webService,以响应您的Android客户端应用程序。请参阅此Link1Link2 for Andoid with php-mysql

希望以上链接可以帮到你。享受编码。