我试图在用户登录时更新SQL数据库中的特定用户信息,我不知道为什么会出现此错误。请帮忙:(
我先查看记录。如果没有,我将添加用户,如果它存在于我的SQL中。不确定我是否正确。
public String RecordCheck(){
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
FirebaseUser user = firebaseAuth.getCurrentUser();
userEmail = user.getEmail().toString();
String RecordHolder = "";
SQLiteDatabase db = getWritableDatabase();
String query = "" + "SELECT * FROM " + TABLE_USERINFO + " WHERE " + COLUMN_USEREMAIL + " = '" + userEmail.trim() + "'";
//Cursor point to a location in your results
Cursor c = db.rawQuery(query, null);
if(c.equals("")){
RecordHolder = "adduser";
}
else
RecordHolder = "updateuser";
db.close();
return RecordHolder;
我在Welcome Back课程中有这个,知道如果用户存在它会更新,如果用户不存在则会添加。
String Record = dbHandler.RecordCheck();
if(Record.equals("adduser")){
dbHandler.addUser(userdata);
}
else {
dbHandler.updateUser(userdata);
}
如果调用更新
public void updateUser(Users user){
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
FirebaseUser userf = firebaseAuth.getCurrentUser();
userEmail = userf.getEmail();
ContentValues values = new ContentValues();
values.put(MyDBHandler.COLUMN_USEREMAIL, user.get_useremail());
values.put(MyDBHandler.COLUMN_NAME, user.getName());
values.put(MyDBHandler.COLUMN_SEX, user.getSex());
values.put(MyDBHandler.COLUMN_BDAY, user.getBday());
values.put(MyDBHandler.COLUMN_HEIGHT, user.getHeight());
values.put(MyDBHandler.COLUMN_MHEIGHT, user.getMheight());
values.put(MyDBHandler.COLUMN_WEIGHT, user.getWeight());
values.put(MyDBHandler.COLUMN_MWEIGHT, user.getMweight());
values.put(MyDBHandler.COLUMN_USEREXPERIENCE, user.getUserexperience());
values.put(MyDBHandler.COLUMN_FQUESTION, user.getFquestion());
values.put(MyDBHandler.COLUMN_SQUESTION, user.getSquestion());
values.put(MyDBHandler.COLUMN_TQUESTION, user.getTquestion());
values.put(MyDBHandler.COLUMN_BODYSTATE, user.getBodystate());
values.put(MyDBHandler.COLUMN_APPARENTLYHEALTHY, user.isApparentlyhealthy());
values.put(MyDBHandler.COLUMN_HYPERTENSION, user.isHypertension());
values.put(MyDBHandler.COLUMN_DIABETES, user.isDiabetes());
values.put(MyDBHandler.COLUMN_ASTHMA, user.isAsthma());
SQLiteDatabase db = getWritableDatabase();
db.update(MyDBHandler.TABLE_USERINFO, values, userEmail, null);
db.close();
}
---------崩溃的开始
11-22 12:44:13.179 17496-17496/com.example.abad.maxfitness2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.abad.maxfitness2, PID: 17496
android.database.sqlite.SQLiteException: near "@gmail": syntax error (code 1): , while compiling: UPDATE users SET fquestion=?,mheight=?,tquestion=?,asthma=?,height=?,bodystate=?,diabetes=?,weight=?,bday=?,useremail=?,squestion=?,hypertension=?,name=?,healthy=?,mweight=?,userexp=?,sex=? WHERE testing8@gmail.com
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1577)
at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1525)
at com.example.abad.maxfitness2.LocalDatabase.MyDBHandler.updateUser(MyDBHandler.java:139)
at com.example.abad.maxfitness2.WelcomeBack$1.onDataChange(WelcomeBack.java:83)
at com.google.android.gms.internal.zzbmz.zza(Unknown Source)
at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source)
at com.google.android.gms.internal.zzboc$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案 0 :(得分:0)
您必须以下面的格式传递where参数
db.update(MyDBHandler.TABLE_USERINFO,values,MyDBHandler.COLUMN_USEREMAIL+"='"+userEmail+"'", null);
答案 1 :(得分:0)
您需要从
更新查询 db.update(MyDBHandler.TABLE_USERINFO, values, userEmail, null);
to:
db.update(MyDBHandler.TABLE_USERINFO, values, "columnId ='"+userEmail+"'", null);
注意:提到的columnId应该是db例如email等
表中列的id