gpt4 book ai didi

Android SQLite INSERT 导致应用程序崩溃

转载 作者:行者123 更新时间:2023-11-29 18:11:36 24 4
gpt4 key购买 nike

我正在尝试将一些数据插入到我创建的表中。我有这个类:

public class DataBase extends SQLiteOpenHelper {
private static final String DB_NAME = "db_mydatabase";
private static final int DB_VERSION = 1;

private static final String TBL_USERS = "users";
private static final String TBL_USERSE_CREATE = "CREATE TABLE IF NOT EXISTS "
+ TBL_USERS + "(id integer PRIMARY KEY AUTOINCREMENT, "
+ "usr varchar(128) NOT NULL UNIQUE, psw varchar(512));";

public DataBase(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}

public List<String> GetUsersList() {
List<String> users = new ArrayList<String>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor c = db.rawQuery("SELECT id, usr FROM " + TBL_USERS, null);
if (c != null ) {
if (c.moveToFirst()) {
do {
String firstName = c.getString(c.getColumnIndex("USR"));
users.add(firstName);
} while (c.moveToNext());
}
}
return users;
}

public void CreateNewUser(String username, String password) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("INSERT INTO " + TBL_USERS + " (usr, psw) VALUES ('" + username + "', '" + password + "')");
//If I comment this line, it doesn't crashes anymore, but I don't see why
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TBL_USERSE_CREATE);
}

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

因此,当我按下 REGISTER 按钮时,来自 EditText 的用户名和密码被发送到 CreateNewUser。我的 Activity 类如下所示:

public class RegisterActivity extends Activity {
List<String> values;
DataBase dtb;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);

ListView listView = (ListView) findViewById(R.id.listView1);

dtb = new DataBase(MainActivity.MainContext);
values = dtb.GetUsersList();

listView.setAdapter(new ArrayAdapter<String>(MainActivity.MainContext, R.layout.listitem, values));

Button Create = (Button) findViewById(R.id.Create);
Create.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
EditText username = (EditText)findViewById(R.id.Username);
EditText password = (EditText)findViewById(R.id.Password);
if (username.getText().length()>0 && password.getText().length()>0) {
if (values.indexOf(username.getText())<0) {
dtb.CreateNewUser(username.getText().toString(), password.getText().toString());
values = dtb.GetUsersList();
}
}
}
});
}

作为 SQLite 程序员的初学者,我遇到了这个可以理解的严重错误和崩溃

> **04-24 12:30:51.340: E/CursorWindow(7986): Bad request for field slot 0,-1. numRows = 1, numColumns = 2** 04-24 12:30:51.350:> D/AndroidRuntime(7986): Shutting down VM 04-24 12:30:51.350:> W/dalvikvm(7986): threadid=3: thread exiting with uncaught exception> (group=0x4001b188) 04-24 12:30:51.350: E/AndroidRuntime(7986):> Uncaught handler: thread main exiting due to uncaught exception> **04-24 12:30:51.380: E/AndroidRuntime(7986): java.lang.IllegalStateException: get field slot from row 0 col -1> failed** 04-24 12:30:51.380: E/AndroidRuntime(7986):  at> android.database.CursorWindow.getString_native(Native Method) 04-24> 12:30:51.380: E/AndroidRuntime(7986):     at> android.database.CursorWindow.getString(CursorWindow.java:329) 04-24> 12:30:51.380: E/AndroidRuntime(7986):     at> android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)> 04-24 12:30:51.380: E/AndroidRuntime(7986):   at> com.bma.myagenda.DataBase.GetUsersList(DataBase.java:32) 04-24> 12:30:51.380: E/AndroidRuntime(7986):     at> com.bma.myagenda.RegisterActivity$1.onClick(RegisterActivity.java:46)> 04-24 12:30:51.380: E/AndroidRuntime(7986):   at> android.view.View.performClick(View.java:2364) 04-24 12:30:51.380:> E/AndroidRuntime(7986):   at> android.view.View.onTouchEvent(View.java:4179) 04-24 12:30:51.380:> E/AndroidRuntime(7986):   at> android.widget.TextView.onTouchEvent(TextView.java:6541) 04-24> 12:30:51.380: E/AndroidRuntime(7986):     at> android.view.View.dispatchTouchEvent(View.java:3709) 04-24> 12:30:51.380: E/AndroidRuntime(7986):     at> android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 04-24> 12:30:51.380: E/AndroidRuntime(7986):     at> android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 04-24> 12:30:51.380: E/AndroidRuntime(7986):     at> android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 04-24> 12:30:51.380: E/AndroidRuntime(7986):     at> android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 04-24> 12:30:51.380: E/AndroidRuntime(7986):     at> android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 04-24> 12:30:51.380: E/AndroidRuntime(7986):     at> com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)> 04-24 12:30:51.380: E/AndroidRuntime(7986):   at> com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)> 04-24 12:30:51.380: E/AndroidRuntime(7986):   at> android.app.Activity.dispatchTouchEvent(Activity.java:2061) 04-24> 12:30:51.380: E/AndroidRuntime(7986):     at> com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)> 04-24 12:30:51.380: E/AndroidRuntime(7986):   at> android.view.ViewRoot.handleMessage(ViewRoot.java:1691) 04-24> 12:30:51.380: E/AndroidRuntime(7986):     at> android.os.Handler.dispatchMessage(Handler.java:99) 04-24> 12:30:51.380: E/AndroidRuntime(7986):     at> android.os.Looper.loop(Looper.java:123) 04-24 12:30:51.380:> E/AndroidRuntime(7986):   at> android.app.ActivityThread.main(ActivityThread.java:4363) 04-24> 12:30:51.380: E/AndroidRuntime(7986):     at> java.lang.reflect.Method.invokeNative(Native Method) 04-24> 12:30:51.380: E/AndroidRuntime(7986):     at> java.lang.reflect.Method.invoke(Method.java:521) 04-24 12:30:51.380:> E/AndroidRuntime(7986):   at> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)> 04-24 12:30:51.380: E/AndroidRuntime(7986):   at> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 04-24> 12:30:51.380: E/AndroidRuntime(7986):     at> dalvik.system.NativeStart.main(Native Method)

最佳答案

改变:

String firstName = c.getString(c.getColumnIndex("USR"));

收件人:

String firstName = c.getString(c.getColumnIndex("usr"));  //USR != usr

关于Android SQLite INSERT 导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10357109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com