SQLite数据库无法正常工作

时间:2012-06-13 06:38:16

标签: android database android-emulator

我正在尝试从用户获取值并使用SQLite数据库显示它。我使用了以下代码,但在模拟器上运行时出现错误“不幸停止”。 DBAdapter.java文件用于创建数据库,result.java使用函数。

result.java

public class result_mode extends Activity implements OnClickListener{
EditText e1, e2, e3, e4, e5;
  DBAdapter db = new DBAdapter(this);
Intent saveIntent;
Intent homeIntent;
Intent abortIntent;
@Override
 public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.res);
  DBAdapter db = new DBAdapter(this);
  getFullList();
        e2 = (EditText)findViewById(R.id.name);
        e1=(EditText)findViewById(R.id.id);
        e3=(EditText)findViewById(R.id.age);
        e5=(EditText)findViewById(R.id.doc);
        e4=(EditText)findViewById(R.id.editText2);
        Button b1 = (Button) findViewById(R.id.abort);
        Button b2 = (Button)findViewById(R.id.home);
        Button b3 = (Button)findViewById(R.id.save);
        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
        b3.setOnClickListener(this);

}

 public void DisplayTitle(Cursor c)
    {
    Toast.makeText(this,
    "UID: " + c.getString(0) + "\n" +
    "NAME: " + c.getString(1) + "\n" +
    "AGE: " + c.getString(2) + "\n" +
    "CONTACT: " + c.getString(3)+ "\n" +
    "Referred By: " + c.getString(4),
    Toast.LENGTH_LONG).show();

    }   
 public void insert() {     

        //insert
        db.open();
        long id;
        id = db.insertPatient(e1.toString(), e2.toString(), e3.toString(), 
                e4.toString(), e5.toString());
        db.close();

 }

 public void getFullList(){

            db.open();
            Cursor c = db.getResult();
            if (c.moveToFirst())
            {
            do {
            DisplayTitle(c);
            } while (c.moveToNext());
            }
            db.close();
     }

 public void handleClick(View v) {
        insert();
        getFullList();
    }


 public void onClick(final View v) {
     switch(v.getId()){
         case R.id.save:
            saveIntent = new Intent(result_mode.this,menuActivity.class);
            startActivity(saveIntent);

         break;
         case R.id.abort:                   
             abortIntent = new Intent(result_mode.this,test_me.class);
            startActivity(abortIntent);
         break;
         case R.id.home:
             homeIntent= new Intent(result_mode.this,TestActivity.class);
            startActivity(homeIntent);

         break;
     }}

}

DBAdapter.java

      public class DBAdapter
      {
        public static final String KEY_UID = "_id";
        public static final String KEY_NAME = "name";
        public static final String KEY_CONTACT = "contact_num";
        public static final String KEY_AGE = "age";
         public static final String KEY_DOC = "doc"; 
        private static final String TAG = "DBAdapter";

        private static final String DATABASE_NAME = "test";
        private static final String DATABASE_TABLE = "table1";
        private static final int DATABASE_VERSION = 1;
        //drop table titles
         private static final String DATABASE_CREATE ="create table patient (" 
     +"ID INTEGER primary key AUTOINCREMENT,"
     + "NAME TEXT,"
     + "AGE NUMBER,"
     + "CONTACT NUMBER,"
     + "DOC TEXT);";
      private final Context context;
    private DatabaseHelper DBHelper;
        private SQLiteDatabase db;

public DBAdapter(Context ctx)
{
    this.context = ctx;
    this.DBHelper = new DatabaseHelper(this.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_NAME); 
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, 
    int newVersion) 
    {               
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }
} 

public DBAdapter open() throws SQLException 
{
    this.db = this.DBHelper.getWritableDatabase();
    return this;
}

public void close() 
{
    this.DBHelper.close();
}

//---insert a title into the database---
public long insertPatient( String id, String name, String age, String contact, String doc )
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_UID, id);
initialValues.put(KEY_CONTACT, contact);
initialValues.put(KEY_AGE, age);
initialValues.put(KEY_DOC, doc);    

return db.insert(DATABASE_TABLE, null, initialValues);
}
/*---deletes a particular patient---
public boolean deletePatient(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID +
"=" + rowId, null) > 0;
}*/
//---retrieves all the titles---
public Cursor getResult()
{
return db.query(DATABASE_TABLE, new String[] {
        KEY_UID ,KEY_NAME, KEY_AGE, KEY_CONTACT,KEY_DOC
},
null,
null,
null,
null,
null);
}
//---retrieves a particular title---
public Cursor getPatient(long id) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {
        KEY_UID ,KEY_NAME, KEY_AGE, KEY_CONTACT,KEY_DOC
}
 , KEY_UID + "=" + id,
          null, null, null, null, null
        );
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//---updates a title---
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}
}

活动日志:

      06-13 12:17:23.581: D/AndroidRuntime(565): Shutting down VM
06-13 12:17:23.581: W/dalvikvm(565): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
06-13 12:17:23.621: E/AndroidRuntime(565): FATAL EXCEPTION: main
06-13 12:17:23.621: E/AndroidRuntime(565): java.lang.RuntimeException: Unable to start activity ComponentInfo{achira.test/achira.test.result_mode}: android.database.sqlite.SQLiteException: Can't downgrade database from version 33 to 1
06-13 12:17:23.621: E/AndroidRuntime(565):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-13 12:17:23.621: E/AndroidRuntime(565):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-13 12:17:23.621: E/AndroidRuntime(565):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-13 12:17:23.621: E/AndroidRuntime(565):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-13 12:17:23.621: E/AndroidRuntime(565):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-13 12:17:23.621: E/AndroidRuntime(565):  at android.os.Looper.loop(Looper.java:137)
06-13 12:17:23.621: E/AndroidRuntime(565):  at android.app.ActivityThread.main(ActivityThread.java:4424)
06-13 12:17:23.621: E/AndroidRuntime(565):  at java.lang.reflect.Method.invokeNative(Native Method)
06-13 12:17:23.621: E/AndroidRuntime(565):  at java.lang.reflect.Method.invoke(Method.java:511)
06-13 12:17:23.621: E/AndroidRuntime(565):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-13 12:17:23.621: E/AndroidRuntime(565):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-13 12:17:23.621: E/AndroidRuntime(565):  at dalvik.system.NativeStart.main(Native Method)
06-13 12:17:23.621: E/AndroidRuntime(565): Caused by: android.database.sqlite.SQLiteException: Can't downgrade database from version 33 to 1
06-13 12:17:23.621: E/AndroidRuntime(565):  at android.database.sqlite.SQLiteOpenHelper.onDowngrade(SQLiteOpenHelper.java:307)
06-13 12:17:23.621: E/AndroidRuntime(565):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:168)
06-13 12:17:23.621: E/AndroidRuntime(565):  at achira.test.DBAdapter.open(DBAdapter.java:62)
06-13 12:17:23.621: E/AndroidRuntime(565):  at achira.test.result_mode.getFullList(result_mode.java:69)
06-13 12:17:23.621: E/AndroidRuntime(565):  at achira.test.result_mode.onCreate(result_mode.java:30)
06-13 12:17:23.621: E/AndroidRuntime(565):  at android.app.Activity.performCreate(Activity.java:4465)
06-13 12:17:23.621: E/AndroidRuntime(565):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-13 12:17:23.621: E/AndroidRuntime(565):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-13 12:17:23.621: E/AndroidRuntime(565):  ... 11 more



 new log :

         06-13 13:01:33.395: E/AndroidRuntime(897):     at                 da   lvik.system.NativeStart.main(Native Method)
         06-13 13:01:33.395: E/AndroidRuntime(897): Caused by: java.lang.reflect.InvocationTargetException
         06-13 13:01:33.395: E/AndroidRuntime(897):     at java.lang.reflect.Method.invokeNative(Native Method)
         06-13 13:01:33.395: E/AndroidRuntime(897):     at java.lang.reflect.Method.invoke(Method.java:511)
         06-13 13:01:33.395: E/AndroidRuntime(897):     at android.view.View$1.onClick(View.java:3039)
         06-13 13:01:33.395: E/AndroidRuntime(897):     ... 11 more
         06-13 13:01:33.395: E/AndroidRuntime(897): Caused by: java.lang.NullPointerException
         06-13 13:01:33.395: E/AndroidRuntime(897):     at achira.test.result_mode.insert(result_mode.java:61)
         06-13 13:01:33.395: E/AndroidRuntime(897):     at achira.test.result_mode.handleClick(result_mode.java:81)
         06-13 13:01:33.395: E/AndroidRuntime(897):     ... 14 more

2 个答案:

答案 0 :(得分:1)

它看起来你改变了private static final int DATABASE_VERSION = 1;从33以前........

根据例外情况

android.database.sqlite.SQLiteException: Can't downgrade database from version 33 to 1

现在只需将版本设置为33或更高。

答案 1 :(得分:0)

onCreate()中,您在execSQL()上调用DATABASE_NAME,该execSQL()被定义为简单的字符串“测试”。 {{1}}的agrument必须是有效的SQL命令。这只是代码中的几个问题之一。