我为我的学校项目编写了代码。当我调用更新方法时会崩溃。错误如下:
E / SQLiteLog:(1)表userInfo没有名为userInfo E / SqDb的列: 错误... android.database.sqlite.SQLiteException:表userInfo没有名为userInfo(代码1):的列,而在编译时:INSERT INTO userInfo(密码,userInfo)值(?,?) 在android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native 方法) 在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:921) 在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:532) 在android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58) 在android.database.sqlite.SQLiteStatement。(SQLiteStatement.java:31) 在android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1570) 在android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1427) 在com.example.prabuddhaabisheka.mock.DBHelper.addInfo(DBHelper.java:46) 在com.example.prabuddhaabisheka.mock.Home $ 2.onClick(Home.java:59) 在android.view.View.performClick(View.java:5232) 在android.view.View $ PerformClick.run(View.java:21289) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:168) 在android.app.ActivityThread.main(ActivityThread.java:5885) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:797) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
public class EditProfile extends AppCompatActivity {
Button search, edit, delete;
EditText name, password, dob;
RadioButton male,female;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_profile);
search = findViewById(R.id.buttonSerach);
edit = findViewById(R.id.buttonEdit);
delete = findViewById(R.id.Delete);
name = findViewById(R.id.editTextName);
password = findViewById(R.id.editTextPassword);
dob = findViewById(R.id.editTextDOB);
male = (RadioButton) findViewById(R.id.radioButtonMale);
female = findViewById(R.id.radioButtonFemale);
}
public void search(View view){
DBHelper dbHelper = new DBHelper(this);
String givenName = name.getText().toString();
ArrayList<userInfo> user ;
user = dbHelper.readAllInfo();
if(!user.isEmpty()) {
for (userInfo u : user) {
if (givenName.equals(u.name)) {
password.setText(u.password);
dob.setText(u.dob);
if ("Male".equals(u.gender)) {
male.setChecked(true);
female.setChecked(false);
} else {
male.setChecked(false);
female.setChecked(true);
}
}
}
}
}
public void update(View view){
DBHelper dbHelper = new DBHelper(this);
String givenName = name.getText().toString();
int id = 0;
ArrayList<userInfo> user;
user = dbHelper.readAllInfo();
if(!user.isEmpty()) {
for (userInfo u : user) {
if (givenName.equals(u.name)) {
id = u.id;
}
}
}
String givenDob = dob.getText().toString();
String givenPassword = name.getText().toString();
String gender;
if (male.isChecked()){
gender = "Male";
}
else{
gender = "Female";
}
boolean updated = dbHelper.updateInfo(id,givenName,givenPassword,givenDob,gender);
if (updated) {
Toast toast = Toast.makeText(this, "Updated", Toast.LENGTH_SHORT);
toast.show();
}
else{
Toast toast = Toast.makeText(this, "Update failed !", Toast.LENGTH_SHORT);
toast.show();
}
}
}
DBHelper类也在下面给出
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, UserProfile.Users.DB_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE "+ UserProfile.Users.TABLE_NAME +" ("+
UserProfile.Users._ID + "INTEGER PRIMARY KEY," +
UserProfile.Users.TABLE_COLUMN_USERNAME + "TEXT,"+
UserProfile.Users.TABLE_COLUMN_PASSWORD + "TEXT," +
UserProfile.Users.TABLE_COLUMN_DOB + "TEXT,"+
UserProfile.Users.TABLE_COLUMN_GENDER + "TEXT)";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public boolean addInfo( ContentValues values ){
SQLiteDatabase db = getWritableDatabase();
long added = db.insert(UserProfile.Users.TABLE_NAME,null,value);
return (added != 0);
}
public boolean updateInfo(int id, String name, String password, String dob, String gender ){
SQLiteDatabase db = getReadableDatabase();
ContentValues values = new ContentValues();
values.put(UserProfile.Users.TABLE_COLUMN_USERNAME,name);
values.put(UserProfile.Users.TABLE_COLUMN_PASSWORD,password);
values.put(UserProfile.Users.TABLE_COLUMN_DOB,dob);
values.put(UserProfile.Users.TABLE_COLUMN_GENDER,gender);
String where = UserProfile.Users._ID + " =";
String[] arg = {Integer.toString(id)};
int row = db.update(UserProfile.Users.TABLE_NAME, values, where, arg);
return (row !=0 );
}
public ArrayList readAllInfo(){
SQLiteDatabase db = getReadableDatabase();
String[] columns = {
UserProfile.Users._ID,
UserProfile.Users.TABLE_COLUMN_USERNAME,
UserProfile.Users.TABLE_COLUMN_PASSWORD,
UserProfile.Users.TABLE_COLUMN_GENDER,
UserProfile.Users.TABLE_COLUMN_DOB
};
Cursor cursor = db.query(UserProfile.Users.TABLE_NAME,columns,null,null,null
,null,null);
ArrayList<userInfo> users = new ArrayList<>();
while (cursor.moveToNext()){
int id = cursor.getInt(cursor.getColumnIndexOrThrow(UserProfile.Users._ID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.TABLE_COLUMN_USERNAME));
String dob = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.TABLE_COLUMN_DOB));
String gender = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.TABLE_COLUMN_GENDER));
String password = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.TABLE_COLUMN_PASSWORD));
userInfo user = new userInfo(id,name,password,dob,gender);
users.add(user);
}
cursor.close();
return users;
}
public ArrayList readAllInfo(int id){
SQLiteDatabase db = getReadableDatabase();
String[] columns = {
UserProfile.Users._ID,
UserProfile.Users.TABLE_COLUMN_USERNAME,
UserProfile.Users.TABLE_COLUMN_PASSWORD,
UserProfile.Users.TABLE_COLUMN_GENDER,
UserProfile.Users.TABLE_COLUMN_DOB
};
String where = UserProfile.Users._ID + " =?";
String[] arg = {Integer.toString(id)};
Cursor cursor = db.query(UserProfile.Users.TABLE_NAME,columns,where,arg,null
,null,null);
ArrayList<userInfo> users = new ArrayList<>();
while (cursor.moveToNext()){
//int ID = cursor.getInt(cursor.getColumnIndexOrThrow(UserProfile.Users._ID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.TABLE_COLUMN_USERNAME));
String dob = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.TABLE_COLUMN_DOB));
String gender = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.TABLE_COLUMN_GENDER));
String password = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.TABLE_COLUMN_PASSWORD));
userInfo user = new userInfo(id,name,password,dob,gender);
users.add(user);
}
cursor.close();
return users;
}
public void deleteUser(int ID){
SQLiteDatabase db = getReadableDatabase();
String where = UserProfile.Users._ID + " =?";
String[] arg = {Integer.toString(ID)};
int count = db.delete(UserProfile.Users.TABLE_NAME,where,arg);
}
}
答案 0 :(得分:0)
错误已清除:
...
.sqlite.SQLiteException: table userInfo has no column named userInfo (code 1):
, while compiling: INSERT INTO userInfo(password,userInfo) VALUES (?,?) at
...
调用方法时
public boolean addInfo( ContentValues values )
您正在传递的内容值是与表同名的值。您必须检查两件事:
UserProfile.Users.TABLE_NAME
的地方(我想它的值是userInfo addInfo
方法使用的内容值的内容。希望有帮助。
答案 1 :(得分:0)
如果要在创建表后向表中添加新行,请尝试删除该表并重新创建。
删除
php excel
此后,您可以重新创建表
答案 2 :(得分:0)
在更改sqlite的On-upgrade方法之后,您必须增加数据库版本。
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(newVersion > oldVersion){
db.execSQL("DROP TABLE IF EXISTS " +......);
db.execSQL("CREATE TABLE IF NOT EXISTS " +......);
insertColumnIfNotAvailableinTable(SQLiteDatabase db, String tableName, String columnName, String columnType);
}
}