我想在其上创建一个示例 SQLite 数据库和操作。
我能够 INSERT 数据进入表格。 但我无法 RETRIEVE / SELECT 数据从db。
显示以下例外
05-09 12:19:26.163: W/dalvikvm(22638): threadid=1: thread exiting with uncaught
exception (group=0x40015560)
05-09 12:19:26.183: E/AndroidRuntime(22638): FATAL EXCEPTION: main
05-09 12:19:26.183: E/AndroidRuntime(22638): java.lang.IllegalStateException: Could not execute method of the activity
05-09 12:19:26.183: E/AndroidRuntime(22638): at android.view.View$1.onClick(View.java:2144)
05-09 12:19:26.183: E/AndroidRuntime(22638): at android.view.View.performClick(View.java:2485)
05-09 12:19:26.183: E/AndroidRuntime(22638): at android.view.View$PerformClick.run(View.java:9080)
05-09 12:19:26.183: E/AndroidRuntime(22638): at android.os.Handler.handleCallback(Handler.java:587)
05-09 12:19:26.183: E/AndroidRuntime(22638): at android.os.Handler.dispatchMessage(Handler.java:92)
05-09 12:19:26.183: E/AndroidRuntime(22638): at android.os.Looper.loop(Looper.java:123)
05-09 12:19:26.183: E/AndroidRuntime(22638): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-09 12:19:26.183: E/AndroidRuntime(22638): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 12:19:26.183: E/AndroidRuntime(22638): at java.lang.reflect.Method.invoke(Method.java:507)
05-09 12:19:26.183: E/AndroidRuntime(22638): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-09 12:19:26.183: E/AndroidRuntime(22638): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-09 12:19:26.183: E/AndroidRuntime(22638): at dalvik.system.NativeStart.main(Native Method)
05-09 12:19:26.183: E/AndroidRuntime(22638): Caused by: java.lang.reflect.InvocationTargetException
05-09 12:19:26.183: E/AndroidRuntime(22638): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 12:19:26.183: E/AndroidRuntime(22638): at java.lang.reflect.Method.invoke(Method.java:507)
05-09 12:19:26.183: E/AndroidRuntime(22638): at android.view.View$1.onClick(View.java:2139)
05-09 12:19:26.183: E/AndroidRuntime(22638): ... 11 more
05-09 12:19:26.183: E/AndroidRuntime(22638): Caused by: android.database.sqlite.SQLiteException: no such table: MYTABLE: , while compiling: SELECT _id, name, password FROM MYTABLE
05-09 12:19:26.183: E/AndroidRuntime(22638): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
05-09 12:19:26.183: E/AndroidRuntime(22638): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
05-09 12:19:26.183: E/AndroidRuntime(22638): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
05-09 12:19:26.183: E/AndroidRuntime(22638): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
05-09 12:19:26.183: E/AndroidRuntime(22638): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
05-09 12:19:26.183: E/AndroidRuntime(22638): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
05-09 12:19:26.183: E/AndroidRuntime(22638): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
05-09 12:19:26.183: E/AndroidRuntime(22638): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1235)
05-09 12:19:26.183: E/AndroidRuntime(22638): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1189)
05-09 12:19:26.183: E/AndroidRuntime(22638): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1271)
05-09 12:19:26.183: E/AndroidRuntime(22638): at com.example.sqlite.MyDatabase.getDetails(MyDatabase.java:32)
05-09 12:19:26.183: E/AndroidRuntime(22638): at com.example.sqlite.MainActivity.getDetails(MainActivity.java:43)
05-09 12:19:26.183: E/AndroidRuntime(22638): ... 14 more
Android 中的代码添加在
下面MainActivity.Java
package com.example.sqlite;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
MyDatabase myDataHelper;
EditText uid,pwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uid = (EditText)findViewById(R.id.editText1);
pwd = (EditText)findViewById(R.id.editText2);
myDataHelper = new MyDatabase(this);
}
public void addUser(View v)
{
String username = uid.getText().toString();
String password = pwd.getText().toString();
long id = myDataHelper.insertData(username, password);
if(id<0)
{
Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "Un Success", Toast.LENGTH_SHORT).show();
}
}
public void getDetails(View v)
{
String data = myDataHelper.getDetails();
Toast.makeText(this,data, Toast.LENGTH_SHORT).show();
}
}
和
MyDatabase.java
package com.example.sqlite;
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.widget.Toast;
public class MyDatabase {
myDataHelper helper;
public MyDatabase(Context context)
{
helper = new myDataHelper(context);
}
public long insertData(String name, String pass){
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(myDataHelper.NAME, name);
cv.put(myDataHelper.PASSWORD, pass);
long id = db.insert(myDataHelper.TABLE_NAME, null, cv);
db.close();
return id;
}
public String getDetails()
{
SQLiteDatabase db = helper.getWritableDatabase();
String[] columns = {myDataHelper.UID,myDataHelper.NAME,myDataHelper.PASSWORD};
Cursor cursor = db.query(myDataHelper.TABLE_NAME, columns, null, null, null, null, null);
StringBuffer buff = new StringBuffer();
while(cursor.moveToNext())
{
int cid = cursor.getInt(0);
String name = cursor.getString(1);
String password = cursor.getString(2);
buff.append(cid+ " "+name+" "+password);
}
return buff.toString();
}
class myDataHelper extends SQLiteOpenHelper
{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "MYDATABASE";
private static final String TABLE_NAME = "MYTABLE";
private static final String UID = "_id";
private static final String NAME = "name";
private static final String PASSWORD = "password";
private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+
" ("+UID+" INTEGER PRIMERY KEY AUTOINCREMENT,"
+NAME+" VARCHAR(225), "
+PASSWORD+" VARCHAR(225));";
private static final String DROP_TABLE = "DROP TABLE IF EXISTS "+"TABLE_NAME";
Context context;
public myDataHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
Message("In Constructor");
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try {
db.execSQL(CREATE_TABLE);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Message("In OnCreate");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
// TODO Auto-generated method stub
try {
db.execSQL(DROP_TABLE);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Message("In onUpgrade");
}
private void Message(String string) {
// TODO Auto-generated method stub
Toast.makeText(context, string, Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:0)
我可以在你的代码中指出两件事。
如果出现问题,您将从onCreate
打印堆栈曲目 - 您是否检查了它?
在onUpgrade
你正在放弃这张桌子。我不确定原因是什么,但我会说您的onUpgrade
方法应该根本不存在或者create table if not exists ...
中包含{{1}}。
答案 1 :(得分:0)
我能够将数据插入表
可能不是。你的吐司信息是相反的。 insert()
会在失败时返回负值,并将其视为成功。
修复插入成功条件。
从数据库帮助程序中删除catch
块。当出现问题时,生命周期回调onCreate()
和onUpgrade()
应该抛出。
卸载并重新安装您的应用,以便删除任何旧版本的数据库文件。