SQLite语法错误代码1,尝试修复,但修复无效

uyhoqukh  于 2023-01-17  发布在  SQLite
关注(0)|答案(1)|浏览(302)

构建成功,但当我尝试通过myDataBaseManager.openDataBase()打开数据库时,应用程序崩溃,并在下面附加错误
有一个崩溃日志:

FATAL EXCEPTION: 
main Process: com.example.smartwardrobe, PID: 9535                                                                                                 
java.lang.RuntimeException: Unable to resume activity 
{com.example.smartwardrobe/com.example.smartwardrobe.LoginActivity}: 
android.database.sqlite.SQLiteException: near "myTableOfClothes": syntax 
error (code 1 SQLITE_ERROR): , while compiling: myTableOfClothes

并且有一个数据库构建类:

public final class MyDataBaseContract {
        public static final String USER_TABLE_NAME = "usersMyTable";
        public static final String USER_ID = "userID";
        public static final String USER_LOGIN = "login";
        public static final String USER_PASSWORD = "password";

        public static final String CLOTHE_TABLE_NAME = "clothesMyTable";
        public static final String CLOTHE_COLOUR = "clothe_Colour";
        public static final String CLOTHE_WARMTH = "clotheWarmth";
        public static final String CLOTHE_TYPE = "clotheType";
        public static final String CLOTHE_NAME = "clotheName";
        public static final String CLOTHE_IMAGE_INDEX = "clotheImageIndex";
        public static final String CLOTHE_OWNER_ID = "ownerId";
        public static final String CLOTHE_ID = "clotheID";

        public static final String DATABASE_NAME = "wardrobeDatabase.db";
        public static final int DB_VERSION = 1;

        public static final String USER_TABLE_STRUCTURE = "CREATE TABLE IF NOT EXISTS " +
                USER_TABLE_NAME + " (" + USER_ID + " INTEGER PRIMARY KEY," + USER_LOGIN + " TEXT," +
                USER_PASSWORD + " TEXT)";
        public static final String CLOTHE_TABLE_STRUCTURE = "CREATE TABLE IF NOT EXISTS " +
                CLOTHE_TABLE_NAME + " (" + CLOTHE_ID + " INTEGER PRIMARY KEY," + CLOTHE_COLOUR + " TEXT," +
                CLOTHE_WARMTH + " TEXT," + CLOTHE_TYPE + " TEXT," + CLOTHE_NAME + " TEXT," +  CLOTHE_IMAGE_INDEX + " TEXT," +
                CLOTHE_OWNER_ID + " TEXT)";

        public static final String DROP_CLOTHE_TABLE = "DROP TABLE IF EXIST " + CLOTHE_TABLE_NAME;
        public static final String DROP_USER_TABLE = "DROP TABLE IF EXIST " + USER_TABLE_NAME;
    }

有一个MyDataBaseManager类:

@SuppressLint("Range")
public class MyDataBaseManager {
    private Context context;
    private MyDataBaseHelper myDataBaseHelper;
    private SQLiteDatabase database;

    public MyDataBaseManager(Context context) {
        this.context = context;
        myDataBaseHelper = new MyDataBaseHelper(context);
    }

    public void openDataBase(){
        database = myDataBaseHelper.getWritableDatabase();
    }

    public void closeDataBase(){
        myDataBaseHelper.close();
    }

    public void insertUserToUserDataBase(String login, String password){
        ContentValues cv = new ContentValues();
        cv.put(MyDataBaseContract.USER_LOGIN, login);
        cv.put(MyDataBaseContract.USER_PASSWORD, password);
        database.insert(MyDataBaseContract.USER_TABLE_NAME, null, cv);
    }

    public void insertClotheToClotheDatabase(String type, String warmth, String colour,
                                             String name, int ownerId){
        ContentValues cv = new ContentValues();
        cv.put(MyDataBaseContract.CLOTHE_TYPE, type);
        cv.put(MyDataBaseContract.CLOTHE_WARMTH, warmth);
        cv.put(MyDataBaseContract.CLOTHE_COLOUR, colour);
        cv.put(MyDataBaseContract.CLOTHE_NAME, name);
        cv.put(MyDataBaseContract.CLOTHE_OWNER_ID, ownerId);
        database.insert(MyDataBaseContract.CLOTHE_TABLE_NAME, null, cv);
    }

    public ArrayList<Clothe> getAllClothesByOwnerId(int ownerId){
        Cursor cursor = database.query(MyDataBaseContract.CLOTHE_TABLE_NAME,
                null, null, null,
                null, null, null);
        ArrayList<Clothe> clothes = new ArrayList<>();

        while (cursor.moveToNext()){
            if(ownerId == cursor.getInt(cursor.getColumnIndex(MyDataBaseContract.CLOTHE_OWNER_ID))){
                Clothe clothe = new Clothe(
                        cursor.getInt(cursor.getColumnIndex(MyDataBaseContract.CLOTHE_ID)),//Clothe ID
                        cursor.getString(cursor.getColumnIndex(MyDataBaseContract.CLOTHE_WARMTH)), //Clothe warmth
                        cursor.getString(cursor.getColumnIndex(MyDataBaseContract.CLOTHE_TYPE)), // Clothe type
                        cursor.getString(cursor.getColumnIndex(MyDataBaseContract.CLOTHE_COLOUR)), //Clothe colour
                        cursor.getString(cursor.getColumnIndex(MyDataBaseContract.CLOTHE_NAME)), //Clothe name
                        cursor.getInt(cursor.getColumnIndex(MyDataBaseContract.CLOTHE_OWNER_ID))); //Owner ID
                clothes.add(clothe);
            }

        }
        cursor.close();
        return clothes;
    }

    public boolean doesLoginExist(String login){
        Cursor cursor = database.query(MyDataBaseContract.USER_TABLE_NAME,
                null, null, null,
                null, null, null);
        while (cursor.moveToNext()){
            if(login.equals(cursor.getString(cursor.getColumnIndex(MyDataBaseContract.USER_LOGIN))))
            {
                cursor.close();
                return true;
            };
        }
        cursor.close();
        return false;
    }

    public User getUserByLogin(String login){
        if (!doesLoginExist(login)) return null;
        Cursor cursor = database.query(MyDataBaseContract.USER_TABLE_NAME,
                null, null, null,
                null, null, null);
        while (cursor.moveToNext()){
            if(login.equals(cursor.getString(cursor.getColumnIndex(MyDataBaseContract.USER_LOGIN)))){
                User user = new User(login, cursor.getString(cursor.getColumnIndex(MyDataBaseContract.USER_PASSWORD)),
                        cursor.getInt(cursor.getColumnIndex(MyDataBaseContract.USER_ID)));
                cursor.close();
                return user;

            }
        }
        cursor.close();
        return null;
    }

    public User getUserByID(int userID){
        Cursor cursor = database.query(MyDataBaseContract.USER_TABLE_NAME,
                null, null, null,
                null, null, null);
        while (cursor.moveToNext()){
            if (userID == cursor.getInt(cursor.getColumnIndex(MyDataBaseContract.USER_ID))){
                User user = new User(cursor.getString(cursor.getColumnIndex(MyDataBaseContract.USER_LOGIN)),
                        cursor.getString(cursor.getColumnIndex(MyDataBaseContract.USER_PASSWORD)),
                        cursor.getInt(cursor.getColumnIndex(MyDataBaseContract.USER_ID)));
                cursor.close();
                return user;
            }
        }
        return null;
    }
}

此外,还有一个使用myDataBaseManager.openDataBase()的类:

public class LoginActivity extends AppCompatActivity {
    private MyDataBaseManager myDataBaseManager;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        //Buttons
        Button btn_login = findViewById(R.id.btn_LoginPage_SignIn);
        Button btn_register = findViewById(R.id.btn_LoginPage_OpenRegisterDialog);
        //Edit Texts
        EditText et_login = findViewById(R.id.et_LoginPage_Login);
        EditText et_password = findViewById(R.id.et_LoginPage_Password);
        //Other
        myDataBaseManager = new MyDataBaseManager(this);
        Activity activity = this;

        btn_register.setOnClickListener(v -> startRegistration(activity, myDataBaseManager));

        btn_login.setOnClickListener(v -> {
            String login = et_login.getText().toString();
            if (myDataBaseManager.doesLoginExist(login)){
                User tempUser = myDataBaseManager.getUserByLogin(login);
                String password = et_password.getText().toString();
                if(tempUser.getPassword().equals(password)){
                    Intent intent = new Intent(activity, MainActivity.class);
                    intent.putExtra("UserID", tempUser.getUserID());
                    startActivity(intent);
                    myDataBaseManager.closeDataBase();
                    finishActivity(0);
                }
                else
                    Toast.makeText(activity, "Password is incorrect. Check it and try again", Toast.LENGTH_SHORT).show();
            }
            else Toast.makeText(activity, "User with this login does not exist. Sign up!(^_^)", Toast.LENGTH_SHORT).show();
        });
    }

    @Override
    protected void onResume() {
       super.onResume();
       myDataBaseManager.openDataBase();
    }

我检查了我的代码几次,在寻找错误的语法数据库或错误的其他类,但没有发现任何东西。
补充信息:虚拟设备:像素6专业版API 33

qxsslcnc

qxsslcnc1#

具体的错误是“myTableOfClothes”附近的语法不正确。看起来您试图使用该名称创建一个表,但它在您共享的代码中不存在,看起来该表名称可能拼写错误,或者您试图创建的表名称不正确。
请检查要创建的表的名称,并检查是否使用了正确的SQL语句来创建该表。在您共享的代码中,MyDataBaseContract类中的表名为“clothesMyTable”,创建该表的语句名为CLOTHE_TABLE_STRUCTURE。
此外,请确保该表尚未创建并且您正尝试再次创建它,如果是这样,则应先删除它,然后再使用DROP_CLOTHE_TABLE语句再次创建它。
同样重要的是要注意,如果在SQL语句中使用硬编码字符串文字,请确保使用正确的表名和列名。
公共静态最终字符串CLOTHE_TABLE_NAME =“衣服我的表”;
公有静态最终字符串CLOTHE_TABLE_STRUCTURE =“如果不存在则创建表格“+ CLOTHE_TABLE_NAME +“(“+衣服标识+“整数主键”,+衣服颜色+“文本”,+衣服温暖+“文本”,+衣服类型+“文本”,+衣服名称+“文本”,+衣服图像索引+“文本”,+衣服所有者标识+“文本")";
此外,您还需要更新代码中使用的所有表名,以匹配正确的表名。
此外,请确保使用正确的SQL

相关问题