我創建了一個數據庫的應用程序,其中包含一個學生表和3個領域,但我不知道如何將該應用程序中的文件添加到sqlite database
。從哪裏把我們的應用程序從sqlitedatabase的文件
數據庫助手:
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATA_BASE="Mydatabase.db";
public static final String TABLE_NAME="Student";
public static final int DATABASE_VERSION =1;
public DatabaseHelper(Context context) {
super(context, DATA_BASE, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME +
"(Name TEXT, AGE NUMERIC, ADDRESS TEXT)"); }
@Override
public void onUpgrade(SQLiteDatabase db,
int oldVersion, int newVersion) {
}
}
主要活動:
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATA_BASE="Mydatabase.db";
public static final String TABLE_NAME="Student";
public static final int DATABASE_VERSION =1;
public DatabaseHelper(Context context) {
super(context, DATA_BASE, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME +
"(Name TEXT, AGE NUMERIC, ADDRESS TEXT)"); }
@Override
public void onUpgrade(SQLiteDatabase db,
int oldVersion, int newVersion) {
}
}
顯示活動:
public class DisplayActivity extends Activity {
private ArrayList<String> arrayList =
new ArrayList<String>();
private SQLiteDatabase Mydatabase;
ListView listView;
EditText enter;
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.display);
listView=(ListView)findViewById(R.id.listView);
//OpenAndQueryDatabase
try {
DatabaseHelper db = new DatabaseHelper(this);
//Open the database
Mydatabase = db.getWritableDatabase();
Cursor c = Mydatabase.rawQuery(
"SELECT * FROM Student", null);
if (c != null) {
if (c.moveToFirst()) {
do {
String Name = c.getString(
c.getColumnIndex("Name"));
int age = c.getInt(
c.getColumnIndex("AGE"));
String address = c.getString(
c.getColumnIndex("ADDRESS"));
arrayList.add("Name : " + Name +"\n"+
"Age : " +age +"\n"+
"Address : " + address+"\n");
}while (c.moveToNext());
}
}
} catch (SQLiteException se) { }
finally {
if (Mydatabase != null)
Mydatabase.close();
}
//displayResultList
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
arrayList));
}
}
我不清楚你想要什麼......你的代碼顯示你正在創建一個空的數據庫......然而你說你想複製一個文件......你是否在你的應用程序之外創建了數據庫,現在想要加載它並在你的應用中使用它? – Barak
不...我創建了我的應用程序..我想在sqlitedatabase.i中查看它在應用程序運行時在emmulator中插入數據..並且我獲得了在列表視圖中輸入到另一個頁面中的所有數據.... 。現在我想看到在SQLite數據庫中輸入的數據 –