我为数据库创建了一个应用程序,其中包含一个学生表和 3 个字段,但我不知道如何将应用程序中的文件添加到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));
}
}