-1
此代碼containg sqlite數據庫中,我havae一個inser按鈕和刪除按鈕,當我們點擊插入按鈕,我想在我的表中插入一行代碼沒有任何錯誤不是它顯示錯誤,但在模擬器中它會顯示強制關閉錯誤我有一個sqlite數據庫,我插入一行,但它會顯示forceclose
package com.android.sample;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter
{
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
// public static final String KEY_FNAME = "fname";
// public static final String KEY_PID = "pid";
// public static final String KEY_SEX = "sex";
//
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "sqldb";
private static final String DATABASE_TABLE = "Mytable";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table Mytable (_id integer primary key autoincrement, "+ "name text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS titles");
onCreate(db);
}
}
//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---insert a title into the database---
public long insertTitle(String name)
{
ContentValues initialValues = new ContentValues();
// initialValues.put(KEY_ROWID, _id);
initialValues.put(KEY_NAME, name);
// initialValues.put(KEY_FNAME, fname);
// initialValues.put(KEY_PID,pid);
// initialValues.put(KEY_SEX,sex);
//
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---deletes a particular title---
public boolean deleteTitle(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID +
"=" + rowId, null) > 0;
}
public Cursor getAllTitles()
{
return db.query(DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_NAME,
},
null,
null,
null,
null,
null);
}
//---retrieves a particular title---
public Cursor getTitle(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_NAME,
},
KEY_ROWID + "=" + rowId,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//---updates a title---
public boolean updateTitle(long _id, String name,
String fname, String pid,String sex)
{
ContentValues args = new ContentValues();
args.put(KEY_ROWID, _id);
args.put(KEY_NAME, name);
return db.update(DATABASE_TABLE, args,
KEY_ROWID , null) > 0;
}
}
SQlit data example
package com.android.sample;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class sqlieDataExample extends Activity {
Button insert_but,cancle_but,retrive_but;
String str;
TextView tv;
DBAdapter db;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sqliteexample);
Intent in=getIntent();
Bundle b=in.getExtras();
if(b!=null)
{
str=b.getString("mudata");
}
db=new DBAdapter(getApplicationContext());
db.open();
insert_but=(Button)findViewById(R.id.insert_but);
cancle_but=(Button)findViewById(R.id.cancle_but);
retrive_but=(Button)findViewById(R.id.retrive_but);
tv=(TextView)findViewById(R.id.textView1);
tv.setText(str);
insert_but.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
db.insertTitle(str);
}});
cancle_but.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//db.deleteTitle(2);
Intent in=new Intent(getApplicationContext(),contactssample.class);
startActivity(in);
// TODO Auto-generated method stub
//finish();
}});
retrive_but.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Cursor cr=db.getAllTitles();
cr.moveToFirst();
do
{
System.out.println("vinod "+cr.getString(1));
}while(cr.moveToNext());
}});
}
}
請提供日誌響應 – Basil 2011-12-28 04:24:00
logcat中應該記錄一些錯誤。奇怪的是,沒有任何日誌應用程序是強制性的。 – kosa 2011-12-28 04:24:50
請發佈logcat錯誤...因爲它可能有助於更好地理解問題......您的「str」變量可能是空的,它可能會引發異常,因爲您在創建表時提到文本不爲空 – 2011-12-28 04:35:48