2013-06-23 89 views
0

仿真器可以訪問數據庫,但在Android設備不工作。 當我打開時,android設備將自動關閉此應用程序。 我正在使用Google API(Google Inc.) - API Level 10仿真器 package ict.mobile;Android的數據庫問題

import android.app.Activity; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteException; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

public class DatabaseMemberQuery extends Activity implements OnClickListener { 
    SQLiteDatabase db; 
String sql; 
Cursor cursor = null; 
String[] columns = { "mid", "name", "password", "age" }; 
TextView tvData; 
String dataStrHeader = String.format("%4s %-12s %-9s %3s\n", "Mid", "Name", "Password", "Age"); 
String dataStr; 
Button btnDBinitial, btnAction; 

public void findView() { 
    tvData = (TextView) findViewById(R.id.data); 
    btnDBinitial = (Button) findViewById(R.id.btnDBinitial); 
    btnAction = (Button) findViewById(R.id.btnAction); 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    findView(); 
    btnDBinitial.setOnClickListener(this); 
    btnAction.setOnClickListener(this); 
    initialDB(); 
} 

public void onClick(View v) { 
    if (v.equals(btnDBinitial)) { 
     initialDB(); 
    } 
    else if (v.equals(btnAction)) { 
     try { 
      db = SQLiteDatabase.openDatabase("/data/data/ict.mobile/MemberDB", null, SQLiteDatabase.OPEN_READONLY); 


      cursor = db.rawQuery("select * from Member where age >=30 order by mid", null); 
      int result = cursor.getCount(); 
      Toast.makeText(this, "Count: " + result, Toast.LENGTH_SHORT).show(); 

      dataStr = dataStrHeader; 
      while (cursor.moveToNext()) { 
       int mid = cursor.getInt(cursor.getColumnIndex("mid")); 
       String name = cursor.getString(cursor.getColumnIndex("name")); 
       String password = cursor.getString(cursor.getColumnIndex("password")); 
       int age = cursor.getInt(cursor.getColumnIndex("age")); 
       dataStr += String.format("%4d %-12s %-9s %3d\n", mid, name, password, age); 

      } 
      tvData.setText(dataStr); 
      db.close(); 
     } catch (SQLiteException e) { 
      Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); 
     } 
    } 
} 

public void initialDB() { 
    try { 

     db = SQLiteDatabase.openDatabase("/data/data/ict.mobile/MemberDB", null, SQLiteDatabase.CREATE_IF_NECESSARY); 
     sql = "DROP TABLE if exists Member;"; 
     db.execSQL(sql); 

     sql= "CREATE TABLE Member(" + "mid int PRIMARY KEY ," + "name text, " 
       + "password text, "+ "age int); "; 

     db.execSQL(sql); 

     db.execSQL("INSERT INTO Member(mid, name, password, age) values" 
       + "(1001, 'Amy Carl', '12345', 16); "); 

     db.execSQL("INSERT INTO Member(mid, name, password, age) values" 
       + "(1002, 'Helen Leung', '88888', 25); "); 

     db.execSQL("INSERT INTO Member(mid, name, password, age) values" 
       + "(1003, 'Robert Chan', 'iloveu', 61); "); 

     db.execSQL("INSERT INTO Member(mid, name, password, age) values" 
       + "(1004, 'Carol Wong', 'peterpan', 33); "); 

     db.execSQL("INSERT INTO Member(mid, name, password, age) values" 
       + "(1005, 'Carman Wong', 'pooh', 44); "); 

     db.execSQL("INSERT INTO Member(mid, name, password, age) values" 
       + "(1006, 'John Chan', 'johnchan', 28); "); 

     db.execSQL("INSERT INTO Member(mid, name, password, age) values" 
       + "(1007, 'Paul Lam', 'apple', 16); "); 

     cursor = db.rawQuery("select * from Member order by mid", null); 

     dataStr = dataStrHeader; 
     while (cursor.moveToNext()) { 
      int mid = cursor.getInt(cursor.getColumnIndex("mid")); 
      String name = cursor.getString(cursor.getColumnIndex("name")); 
      String password = cursor.getString(cursor.getColumnIndex("password")); 
      int age = cursor.getInt(cursor.getColumnIndex("age")); 
      dataStr += String.format("%4d %-12s %-9s %3d\n", mid, name, password, age); 
     } 
     tvData.setText(dataStr); 
     Toast.makeText(this, "Table Member is created and initialised.", Toast.LENGTH_SHORT).show(); 
     db.close(); 
    } 
    catch (SQLiteException e) { 
     Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); 
    } 
} 

}

+1

顯示的logcat輸出:http://developer.android.com/tools/help/logcat.html –

回答

0

你的數據庫的路徑應該是

/data/data/ict.mobile/databases/MemberDB

According to docs

要使用sqlite3的,進入仿真器實例的遠程shell,如所描述上面,然後使用sqlite3命令調用該工具。或者,在調用sqlite3時,您可以指定要探索的數據庫的完整路徑。仿真器/設備實例存儲的SQLite3數據庫的文件夾/數據/數據/ <程序包名稱> /數據庫/英寸

下面是一個例子:

adb -s emulator-5554 shell 
# sqlite3 /data/data/com.example.google.rss.rssexample/databases/rssitems.db` 

之前執行的應用程序與新的路徑刪除模擬器/設備您的應用程序。

+0

它的作品!非常感謝你 –