我第一次在 Android 中使用 sqlite 数据库,我在 Sqlite 中创建了我的数据库并将其复制到 android“资产”,我想通过“单击”“按钮”从数据库中获取数据,我有一个表包含4 个字段“ID、位置、纬度、经度”,但我无法从中获取任何数据。在我的 xml 中,我为插入数据“纬度”和“经度”制作了两个“编辑文本”,然后使用“搜索”按钮从数据库中获取数据位置。但我无法获得任何数据。我是安卓新手,请帮帮我
在这里我发布我的代码
数据库适配器
package com.joel.databases;
import java.io.IOException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class DBAdapter {
private static String DB_PATH = "/data/data/com.tracking/databases/";
private static String DB_NAME = "tracking.sqlite";
private SQLiteDatabase db;
//private final Context myContext;
private DBAdapter DBHelper;
private final Context context;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DBAdapter(context);
//super(context, DB_NAME, null, 1);
//this.myContext = context;
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (!dbExist) {
/*By calling this method and empty database will be created into the default system path
of your application so we are gonna be able to overwrite that database with our database.
*/
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database file.");
}
}
}
private void getReadableDatabase() {
// TODO Auto-generated method stub
}
public boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
InputStream myinput =
context.getApplicationContext().getAssets().open(DB_NAME);
OutputStream myoutput = new FileOutputStream("/data/data/com.tracking
/databases/"+ context.getApplicationContext().getPackageName() +"/databases
/tracking.sqlite");
byte[] buffer = new byte[1024];
int length;
while ((length = myinput.read(buffer)) > 0) {
myoutput.write(buffer, 0, length);
}
myoutput.flush();
myoutput.close();
myinput.close();
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
public synchronized void close() {
if(db != null) db.close();
db.close();
}
public void onCreate(SQLiteDatabase db) {}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
}
这是我的database.java
package com.joel.databases;
//import com.google.android.maps.MapView;
import com.joel.databases.R;
import android.app.Activity;
import android.os.Bundle;
import java.io.IOException;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.database.Cursor;
public class databases extends Activity {
/** Called when the activity is first created. */
private Button cari;
private databases this_class = this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DBAdapter db = new DBAdapter(this);
db.openDataBase();
cari = (Button) findViewById(R.id.cari);
// cari.setOnClickListener(new Button.OnClickListener(){
//public void onClick(View v){
InitDatabase();
}
public void InitDatabase() {
AsyncTask<String, Void, String> InitDB = new AsyncTask<String, Void, String>() {
Dialog progress = null;
String msg;
DBAdapter db_adapter;
@Override
protected void onPreExecute() {
db_adapter = new DBAdapter(this_class);
if (!db_adapter.checkDataBase())
progress = ProgressDialog.show(this_class, "", "Installing
Database.\nPlease wait.");
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try {
db_adapter.createDataBase();
msg = "Database successfully installed.";
} catch (IOException ioe) {
msg = "Database installation failed.";
}
return msg;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (progress!=null) {
progress.dismiss();
Toast.makeText(getApplicationContext(), result,
Toast.LENGTH_SHORT).show();
}
}
};
InitDB.execute(new String());
}
}