2012-05-09 163 views
1

我正在与Android的sqlite数据库第一次,我已经创建我的数据库在Sqlite并将其复制到Android“资产”,我想从数据库中获取数据与“点击”“按钮”,我有一个表包含4个字段“ID,位置,纬度,经度”,但我不能从中获取任何数据。 在我的XML中,我做了两个“编辑文本”插入数据“lattitude”和“经度”,然后按钮“搜索”从数据库中获取数据位置。 但我不能得到任何数据。 即时通讯新至Android 请帮我如何从ANDROID中的sqlite数据库中获取数据?

这里我发表我的代码

将对DBAdapter

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

     } 
+0

请发布任何logcat错误。 – Sam

+0

你有一个扩展到SQLiteOpenHelper的类吗?因为SQLiteOpenHelper允许你创建实际的数据库,查询它们等等。 – sdfwer

+0

看看下面的链接上的Android教程 Android应用开发教程在Android应用中从Sqlite中检索数据 [http://jannatyahan.com/android-app-development-tutorial-to-retrieve-data-from -SQLite功能于机器人/(http://jannatyahan.com/android-app-development-tutorial-to-retrieve-data-from-sqlite-in-android/) –

回答

1

使用

OutputStream myoutput = new FileOutputStream(DB_PATH + DB_NAME); 

代替

OutputStream myoutput = new FileOutputStream("/data/data/com.tracking 
    /databases/"+ context.getApplicationContext().getPackageName() +"/databases 
    /tracking.sqlite"); 
0

很明显,你试图达到的远远超出你的水平 - 我的诚实和不需要的建议是:花些时间在简单的平台上编程简单的东西。通过这种方式,您无需一直向人们寻求帮助,就可以继续前进。而独自行动是唯一能让你学习的东西。然后学习一些Java(只是基础知识,但是围绕它的语言和位,如命名约定)。

然后回到Android。突然,所有的教程和api文档将开始对你有意义。

相关问题