gpt4 book ai didi

java - SQLite (1) 没有这样的表

转载 作者:行者123 更新时间:2023-11-29 07:48:15 25 4
gpt4 key购买 nike

当我运行我的 android 应用程序时,我不断收到错误 (1) no such table userTable,据我所知,我正在

TABLE_CREATE = "create table userTable("    
+ USER + " text not null, " + PWD + " text not null, );";

我在这个项目中还有另外两个数据库,它们工作正常,欢迎任何帮助。

package com.weightpro.db;

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.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class usrPwdDB {

public static final String USER = "userName";
public static final String PWD = "password";
public static final String TABLE_NAME = "userTable";
public static final String DATA_BASE_NAME = "userdatabase";
public static final String KEY_ROWID = "_id";
public static final int DB_VERSION = 2;
private static final String TABLE_CREATE = "create table userTable("
+ USER + " text not null, " + PWD + " text not null, );";

DBHelper WDBHelper;
Context mContext;
SQLiteDatabase db;

public usrPwdDB(Context mContext) {
this.mContext = mContext;
WDBHelper = new DBHelper(mContext);

}

private static class DBHelper extends SQLiteOpenHelper
{

public DBHelper(Context context) {
super(context,DATA_BASE_NAME, null, DB_VERSION);

}

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

catch(SQLException e)
{
e.printStackTrace();
}
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

db.execSQL("DROP TABLE IF EXISTS userTable");
onCreate(db);
}


}
public usrPwdDB open()
{
db = WDBHelper.getWritableDatabase();
return this;

}

public void close(){
WDBHelper.close();
}

public long insertInfo(String userName, String password){
ContentValues content = new ContentValues();
content.put(USER, userName);
content.put(PWD, password);
return db.insertOrThrow(TABLE_NAME, null, content);
}

public boolean getUserNameAndPassword(String userName, String Password) throws SQLException {

Cursor mCursor =
db.query(true, TABLE_NAME, new String[] {USER,
PWD},USER+"='"+userName+"' AND password='"+Password+"'", null,
null, null, null, null);

if (mCursor.getCount() > 0)
{
return true;
}
return false;}

public Cursor returnData(){
return db.query(TABLE_NAME, new String[] {USER, PWD},null,null,null,null,null);
}
}

最佳答案

从创建表语法中删除最后一个逗号 ,,

private static final String TABLE_CREATE = 
"create table userTable("
+ USER + " text not null, "
+ PWD + " text not null, );"; // remove this comma after not null

正确的语法应该是

private static final String TABLE_CREATE = 
"create table userTable("
+ USER + " text not null, "
+ PWD + " text not null );";

关于java - SQLite (1) 没有这样的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23515503/

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