我在android studio中创建应用程序,我创建一个表来存储用户名、电子邮件、密码和用户id。用户名、电子邮件和密码通过用户的输入传递,而id由计算机自动生成,当我为这些信息创建表时,上面的3列工作正常,但我不能创建用户id列。
下面是我创建数据库表的代码:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME ="contacts.db";
private static final String TABLE_NAME ="contacts";
private static final String COLUMN_ID ="id";
private static final String COLUMN_NAME ="name";
private static final String COLUMN_EMAIL ="email";
private static final String COLUMN_PASS ="pass";
SQLiteDatabase db;
private static final String TABLE_CREATE = "create table contacts (id INTEGER primary key NOT NULL ," +
"name text not null, email text not null, pass text not null);";
public DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) { // create database
db.execSQL(TABLE_CREATE);
this.db = db;
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
String query = "DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(query);
this.onCreate(db);
}
public void insertContact(Contact c){
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
String query = "select * from contacts";
Cursor cursor = db.rawQuery(query, null);
int count = cursor.getCount();
values.put(COLUMN_ID, count);
values.put(COLUMN_NAME, c.getUser_name());
values.put(COLUMN_EMAIL, c.getUser_email());
values.put(COLUMN_PASS, c.getUser_password());
db.insert(TABLE_NAME, null, values); // insert into database
db.close();
}
public String searchPass(String uname){
db = this.getReadableDatabase();
String query = "select uname, pass from " + TABLE_NAME;
Cursor cursor = db.rawQuery(query, null);
String a, b;
b = "not found";
if (cursor.moveToFirst()){
do {
a = cursor.getString(0);
if (a.equals(uname)){
b = cursor.getString(1);
break;
}
}while (cursor.moveToNext());
}
return b;
}
当我测试我的应用程序时,我将其录制到“name=,email=,password=”,它显示以下问题:
07-12 15:26:27.621 7943-7943/com.example.haoch.myapplication e/sqlitelog:(1)表联系人没有名为id的列07-12 15:26:27.625 7943-7943/com.example.haoch.myapplication e/sqlitedatabase:插入email= name= pass= id=0 android.database.sqlite.sqliteexception:表联系人没有名为id的列(代码1):,编译时:插入联系人(email、name、pass、id)值(?、?、?、?)
1条答案
按热度按时间42fyovps1#
你改变数据库结构了吗?如果是这种情况,每次更改数据库结构时,都必须增加数据库版本:
或者,您可以重新安装应用程序或清除设备上的应用程序数据