2012-05-09 15 views
1

私は初めてAndroid用のsqliteデータベースを使って作業しています。Sqliteでデータベースを作成し、アンドロイド "資産"にコピーしました。 "ボタンをクリック"してデータを取得したい、テーブルには4つのフィールド "ID、Location、Lattitude、Longitude"が含まれていますが、データは取得できません。 私のxmlで、私は "lattitude"と "経度"を挿入するための2つの "編集テキスト"を作成し、データベースからデータの場所を取得する "検索"ボタン。 でもデータを取得できません。 を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

ログキャストエラーを投稿してください。 – Sam

+0

SQLiteOpenHelperに拡張されたクラスがありますか? SQLiteOpenHelperを使用すると、実際のデータベースを作成したり、クエリを実行したりすることができます。 – sdfwer

+0

下記リンクのアンドロイドチュートリアルをご覧ください Android App開発チュートリアルAndroid AppのSqliteからデータを取得する [http://jannatyahan.com/android-app-development-tutorial-to-retrieve-data-from -sqlite-in-android /](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ドキュメントが意味をなさないでしょう。

関連する問題