我试图在我的数据库中添加一些信息时遇到问题。 这是我的DateBase类:
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper{
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "contactDb";
public static final String TABLE_CONTACTS = "contacts";
public static final String KEY_ID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_MAIL = "mail";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_CONTACTS + " (" + KEY_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME + " TEXT NOT NULL, " + KEY_MAIL + " TEXT NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists " + TABLE_CONTACTS);
onCreate(db);
}
}
这是我的课程,我试图在表格中添加信息:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button btnAdd, btnRead, btnClear;
EditText etName, etEmail;
DBHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(this);
btnRead = (Button) findViewById(R.id.btnRead);
btnRead.setOnClickListener(this);
btnClear = (Button) findViewById(R.id.btnClear);
btnClear.setOnClickListener(this);
etName = (EditText) findViewById(R.id.etName);
etEmail = (EditText) findViewById(R.id.etEmail);
dbHelper = new DBHelper(this);
}
@Override
public void onClick(View v) {
String name = etName.getText().toString();
String email = etEmail.getText().toString();
SQLiteDatabase database = dbHelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
switch (v.getId()) {
case R.id.btnAdd:
contentValues.put(DBHelper.KEY_NAME, name);
contentValues.put(DBHelper.KEY_MAIL, email);
database.insert(DBHelper.TABLE_CONTACTS, null, contentValues);
break;
case R.id.btnRead:
Cursor cursor = database.query(DBHelper.TABLE_CONTACTS, null, null, null, null, null, null);
if (cursor.moveToFirst()) {
int idIndex = cursor.getColumnIndex(DBHelper.KEY_ID);
int nameIndex = cursor.getColumnIndex(DBHelper.KEY_NAME);
int emailIndex = cursor.getColumnIndex(DBHelper.KEY_MAIL);
do {
Log.d("mLog", "ID = " + cursor.getInt(idIndex) +
", name = " + cursor.getString(nameIndex) +
", email = " + cursor.getString(emailIndex));
} while (cursor.moveToNext());
} else
Log.d("mLog","0 rows");
cursor.close();
break;
case R.id.btnClear:
database.delete(DBHelper.TABLE_CONTACTS, null, null);
break;
}
dbHelper.close();
}
}
这是完整的错误文字:
E/SQLiteLog: (1) no such table: contacts
E/SQLiteDatabase: Error inserting name=hey mail=hey@mail.ru
android.database.sqlite.SQLiteException: no such table: contacts (code 1): , while compiling: INSERT INTO contacts(name,mail) VALUES (?,?)
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.insertWithOnConflict(SQLiteDatabase.java:1472)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1343)
at com.lol.sqlite.MainActivity.onClick(MainActivity.java:67)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
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:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
很高兴看到任何答案