无法将数据插入到SQLite数据库的Android设备 [英] unable to insert data into sqlite database for android device
问题描述
我遇到未知错误,同时将数据插入到数据库中。在LogCat中一直显示无法插入数据,但是,我用从其他活动(其中工程)复制code。
奇怪的是,当我把code。在 ONSTART()
的方法,它的工作原理。但是当我放入我的的onClick()
,它失败了。
ONSTART()
:其中, - 不知道是否它的相关的误差
@覆盖
公共无效的OnStart(){
super.onStart();
dbopener =新DB_Route_ListOpener(本);
尝试 {
dbopener.createDatabase();
}赶上(IOException异常IOE){
抛出新的错误(无法创建数据库);
} //创建数据库
尝试 {
dbopener.openDatabase();
}赶上(的SQLException SQLE){
扔SQLE;
} //打开数据库
//配置列表视图
routeItems =新的ArrayList<字符串>(dbopener.selectData(新的String [] {RouteName},NULL,NULL,NULL,NULL,NULL));
routeListAdapter =新的ArrayAdapter<字符串>(这一点,android.R.layout.simple_list_item_1,routeItems);
routeListItem =(ListView控件)this.findViewById(R.id.mlt_route_list_route);
routeListItem.setAdapter(routeListAdapter);
routeListItem.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
dbopener.close();
} // ONSTART
的onClick()
:
@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
如果(V == findViewById(R.id.mlt_route_btn_addroute))//添加
{
AlertDialog.Builder建设者=新AlertDialog.Builder(本);
builder.setCancelable(假);
builder.setMessage(添加名字?);
builder.setPositiveButton(是,新DialogInterface.OnClickListener()
{
公共无效的onClick(DialogInterface对话,诠释其)
{
// TODO自动生成方法存根
dialog.dismiss();
ContentValues CV =新ContentValues();
cv.put(RouteName,测试);
dbopener.insertData(NULL,CV);
}
}); //清除所有数据
builder.setNegativeButton(取消,新DialogInterface.OnClickListener()
{
公共无效的onClick(DialogInterface对话,诠释其)
{
// TODO自动生成方法存根
dialog.cancel();
}
});//负
builder.create()显示()。
}//如果
我的插入查询:
公共无效insertData(字符串nullColumnHack,ContentValues值){
尝试
{
myDatabase.insert(DB_TABLE,nullColumnHack,价值观);
}赶上(例外五){
Log.e(错误:,无法插入数据);
}//抓住
} // insertData
您需要设置 dbopener
作为一个全局变量并移动 dbopener。关闭();
从的OnStart()
到的OnDestroy()
,因为你关闭了数据库在插入数据之前
I've met an unknown error while inserting data into the database. The LogCat had been display "unable to insert data", however, the code that I've used is copied from another activity (which works).
weird enough, when I placed the code in the onStart()
method, it works. but when I placed into my onClick()
, it failed.
onStart()
: <- not sure whether it's related to the error
@Override
public void onStart() {
super.onStart();
dbopener = new DB_Route_ListOpener(this);
try {
dbopener.createDatabase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}//create database
try {
dbopener.openDatabase();
}catch(SQLException sqle){
throw sqle;
}//open database
// Configure the listview
routeItems = new ArrayList<String>(dbopener.selectData(new String [] {"RouteName"}, null, null, null, null, null));
routeListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,routeItems);
routeListItem = (ListView)this.findViewById(R.id.mlt_route_list_route);
routeListItem.setAdapter(routeListAdapter);
routeListItem.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
dbopener.close();
}//onStart
onClick()
:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == findViewById(R.id.mlt_route_btn_addroute))//add
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage("Add name ?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
dialog.dismiss();
ContentValues cv = new ContentValues();
cv.put("RouteName", "test");
dbopener.insertData(null, cv);
}
});//clear all data
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
dialog.cancel();
}
});//negative
builder.create().show();
}//if
my insert query:
public void insertData (String nullColumnHack, ContentValues values) {
try
{
myDatabase.insert(DB_TABLE, nullColumnHack, values);
} catch (Exception e) {
Log.e("Error :","unable to insert data");
}//catch
}//insertData
You need to set the dbopener
as a global variable and move dbopener.close();
from OnStart()
to OnDestroy()
since you closed the DB before you insert the data
这篇关于无法将数据插入到SQLite数据库的Android设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!