How do I connect an SQLite database (SQLite Firefox extension) to an Android application, for android 2.1 to query and insert to the database
I am working on an Android application, where some of the functions that users should input: register, locate hospital location, search for messages (sent via sms to registered users) and can schedule and make appointments.
So far, all the activities are done. However, I need to know how to connect to the database in order to read and store this data. I am using Android 2.1, Mac OS X, and the SQLite Firefox extension.
This is the code I have to connect to the SQLite database, what am I doing wrong?
package com.bitnuts.imom;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DbManager extends SQLiteOpenHelper {
private static final String DB_NAME = "iMom.sqlite";
private static final String DB_PATH = "/data/data/com.bitnuts.imom/databases/";
private static final Integer DB_VERSION = 1;
private static final String TAG = "DbManager";
private final Context context;
private SQLiteDatabase db;
private DbManager dbManager;
public DbManager(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.context = context;
}
public DbManager open() {
dbManager = ne开发者_Python百科w DbManager(context);
db = dbManager.getWritableDatabase();
return this;
}
@Override
public void onCreate(SQLiteDatabase arg0) {
createNewDatabase();
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
public void createNewDatabase() {
InputStream assetsDB = null;
try {
assetsDB = context.getAssets().open(DB_NAME);
OutputStream dbOut = new FileOutputStream(DB_PATH + DB_NAME);
byte[] buffer = new byte[1024];
int length;
while ((length = assetsDB.read(buffer)) > 0) {
dbOut.write(buffer, 0, length);
}
dbOut.flush();
dbOut.close();
assetsDB.close();
}
catch (IOException e) {
}
}
}
You cannot use the SQLite database directly in your application. You need to make some modifications. Such as the android_metadata
table, etc. The blog post Using your own SQLite database in Android applications will help you.
There is no JDBC driver on Android, so you can not connect to databases on the network (SQLite or other): Android - Access to online Database SQlite
Android comes with an embedded SQLite database, but this is available only within the device.
Also, read why direct connection to databases from mobile devices is not recommended.
If you need to store/share some data on the network, then the usual way is to create a web service on the server that all devices can connect to. A very popular type of web service is REST, which is quite easy to set up in Java.
I suggest to use ORMLite for Android. The implementation becomes a lot simpler with it. Here's the getting started documentation.
精彩评论