Java android应用程序开发中的SQLite数据库行为
您好,我尝试了以下代码,并在列表视图中正确地将表中的记录列表作为输出 但是,当我在Main_Activity类中注释以下两个方法时,数据库记录不会被加载。 createTable(); insertData() 我在想,当我们关闭同一个应用程序后打开它时,是否必须再次创建表并插入相同的记录集 有人知道为什么会这样吗。。。谢谢Java android应用程序开发中的SQLite数据库行为,java,android,sqlite,Java,Android,Sqlite,您好,我尝试了以下代码,并在列表视图中正确地将表中的记录列表作为输出 但是,当我在Main_Activity类中注释以下两个方法时,数据库记录不会被加载。 createTable(); insertData() 我在想,当我们关闭同一个应用程序后打开它时,是否必须再次创建表并插入相同的记录集 有人知道为什么会这样吗。。。谢谢 public class Main_Activity extends ListActivity { private static String SAMPLE_TABLE_
public class Main_Activity extends ListActivity {
private static String SAMPLE_TABLE_NAME = "PERSONS_TABLE";
private SQLiteDatabase sampleDB = null;
private List<String> results = new ArrayList<String>();
private Cursor cursor = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try
{
sampleDB = openOrCreateDatabase("NAME", MODE_PRIVATE, null);
createTable();
insertData();
lookupData();
this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));
}
catch (SQLiteException se)
{
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
}
finally
{
if (sampleDB != null)
sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAME);
sampleDB.close();
}
}
private void createTable()
{
sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
SAMPLE_TABLE_NAME +
" (PERSON_NAME VARCHAR, " +
" COUNTRY VARCHAR, " +
" AGE INT(3));");
}
private void insertData()
{
sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values ('James','ENGLAND',25);");
sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values ('Dave','USA',18);");
sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values ('Jean-Paul','FRANCE',33);");
sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values ('Sergio','SPAIN',42);");
sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values ('Hitori','JAPAN',73);");
}
private void lookupData()
{
cursor = sampleDB.rawQuery("SELECT PERSON_NAME, COUNTRY, AGE FROM " +
SAMPLE_TABLE_NAME +
" where AGE > 10 ", null);
if (cursor != null)
{
if (cursor.moveToFirst())
{
do
{
String personName = cursor.getString(cursor.getColumnIndex("PERSON_NAME"));
String country = cursor.getString(cursor.getColumnIndex("COUNTRY"));
int age = cursor.getInt(cursor.getColumnIndex("AGE"));
results.add("" + personName + ", " + country + ", " + age);
}
while (cursor.moveToNext());
}
cursor.close();
}
}
}
公共类主活动扩展了ListActivity{
私有静态字符串SAMPLE\u TABLE\u NAME=“PERSONS\u TABLE”;
私有SQLiteDatabase sampleDB=null;
私有列表结果=新建ArrayList();
私有游标=null;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
尝试
{
sampleDB=openOrCreateDatabase(“NAME”,MODE_PRIVATE,null);
createTable();
insertData();
lookUpdatea();
setListAdapter(新的ArrayAdapter(这个,android.R.layout.simple_list_item_1,results));
}
catch(sqlitese异常)
{
Log.e(getClass().getSimpleName(),“无法创建或打开数据库”);
}
最后
{
if(sampleDB!=null)
execSQL(“从”+样本表名称中删除);
sampleDB.close();
}
}
私有void createTable()
{
execSQL(“如果不存在,则创建表”+
示例\u表\u名称+
(姓名为VARCHAR的人)+
“乡村瓦查尔,”+
“AGE INT(3));”;
}
私有void insertData()
{
execSQL(“插入到“+SAMPLE_TABLE_NAME+”值('James','ENGLAND',25);”);
execSQL(“插入到“+SAMPLE_TABLE_NAME+”值('Dave','USA',18);”;
execSQL(“插入到“+SAMPLE_TABLE_NAME+”值('Jean-Paul','FRANCE',33);”;
execSQL(“插入到“+SAMPLE_TABLE_NAME+”值('Sergio','西班牙',42);”;
execSQL(“插入到“+SAMPLE_TABLE_NAME+”值('Hitori','JAPAN',73);”;
}
私有void lookupdatea()
{
cursor=sampleDB.rawQuery(“选择人名、国家、年龄从”+
示例\u表\u名称+
“年龄>10岁者”,空);
如果(光标!=null)
{
if(cursor.moveToFirst())
{
做
{
String personName=cursor.getString(cursor.getColumnIndex(“人名”);
字符串country=cursor.getString(cursor.getColumnIndex(“国家”);
int age=cursor.getInt(cursor.getColumnIndex(“age”);
结果。添加(“+人名+”,“+国家+”,“+年龄);
}
while(cursor.moveToNext());
}
cursor.close();
}
}
}
在创建DB的try语句中,填充它。。。然后在finally语句中删除所有记录。这就是为什么你的数据库总是空的。您应该记住,无论如何,在try或catch之后,finally都会执行