学生テーブルと3つのフィールドを含むデータベース用のアプリを1つ作成しましたが、アプリ内のファイルをに追加する方法がわかりませんsqlite database
。
データベースヘルパー:
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));
}
}