所以我创建了一个数据库,由于下面的错误,我做错了,但我不确定导致错误的原因。我知道它在我的DATABASE_CREATE变量中,但我不知道我的格式错误。任何帮助,将不胜感激。这是我的错误
06-01 19:41:11.943: E/SQLiteLog(15521): (1) no such table: YCDB
06-01 19:41:11.948: E/SQLiteDatabase(15521): Error inserting users=john password=mavsman
06-01 19:41:11.948: E/SQLiteDatabase(15521): android.database.sqlite.SQLiteException: no such table: YCDB (code 1): , while compiling: INSERT INTO YCDB(users,password) VALUES (?,?)
06-01 19:41:11.948: E/SQLiteDatabase(15521): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
06-01 19:41:11.948: E/SQLiteDatabase(15521): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1118)
06-01 19:41:11.948: E/SQLiteDatabase(15521): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:691)
06-01 19:41:11.948: E/SQLiteDatabase(15521): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
06-01 19:41:11.948: E/SQLiteDatabase(15521): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
06-01 19:41:11.948: E/SQLiteDatabase(15521): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
06-01 19:41:11.948: E/SQLiteDatabase(15521): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1589)
06-01 19:41:11.948: E/SQLiteDatabase(15521): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1461)
06-01 19:41:11.948: E/SQLiteDatabase(15521): at yc.android.yourchallenger.DBAdapter.insertContact(DBAdapter.java:81)
06-01 19:41:11.948: E/SQLiteDatabase(15521): at yc.android.yourchallenger.MainActivity.onCreate(MainActivity.java:27)
06-01 19:41:11.948: E/SQLiteDatabase(15521): at android.app.Activity.performCreate(Activity.java:5372)
06-01 19:41:11.948: E/SQLiteDatabase(15521): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
06-01 19:41:11.948: E/SQLiteDatabase(15521): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
06-01 19:41:11.948: E/SQLiteDatabase(15521): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
06-01 19:41:11.948: E/SQLiteDatabase(15521): at android.app.ActivityThread.access$700(ActivityThread.java:168)
06-01 19:41:11.948: E/SQLiteDatabase(15521): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
06-01 19:41:11.948: E/SQLiteDatabase(15521): at android.os.Handler.dispatchMessage(Handler.java:99)
06-01 19:41:11.948: E/SQLiteDatabase(15521): at android.os.Looper.loop(Looper.java:137)
06-01 19:41:11.948: E/SQLiteDatabase(15521): at android.app.ActivityThread.main(ActivityThread.java:5493)
06-01 19:41:11.948: E/SQLiteDatabase(15521): at java.lang.reflect.Method.invokeNative(Native Method)
06-01 19:41:11.948: E/SQLiteDatabase(15521): at java.lang.reflect.Method.invoke(Method.java:525)
06-01 19:41:11.948: E/SQLiteDatabase(15521): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
06-01 19:41:11.948: E/SQLiteDatabase(15521): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
06-01 19:41:11.948: E/SQLiteDatabase(15521): at dalvik.system.NativeStart.main(Native Method)
06-01 19:41:12.188: D/libEGL(15521): loaded /system/lib/egl/libEGL_mali.so
06-01 19:41:12.193: D/libEGL(15521): loaded /system/lib/egl/libGLESv1_CM_mali.so
06-01 19:41:12.203: D/libEGL(15521): loaded /system/lib/egl/libGLESv2_mali.so
06-01 19:41:12.218: E/(15521): Device driver API match
06-01 19:41:12.218: E/(15521): Device driver API version: 23
06-01 19:41:12.218: E/(15521): User space API version: 23
06-01 19:41:12.218: E/(15521): mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Wed Oct 9 21:05:57 KST 2013
06-01 19:41:12.473: D/OpenGLRenderer(15521): Enabling debug mode 0
这是数据库类 包yc.android.yourchallenger;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
static final String KEY_ROWID = "id";
static final String KEY_USER = "user";
static final String KEY_PASSWORD = "password";
static final String TAG = "DBAdapter";
static final String DATABASE_NAME = "YCDB";
static final String KEY_NAME = "users";
static final int DATABASE_VERSION = 1;
static final String DATABASE_CREATE =
"create table contact " + KEY_USER + KEY_PASSWORD;
final Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;
public DBAdapter (Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_CREATE);
}
catch(SQLException e){
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to " +
newVersion + ", which will " +
"destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
}
//opens db database
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return null;
}
//closes the database
public void close()
{
DBHelper.close();
}
//insert contacts
public long insertContact(String user, String password )
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, user);
initialValues.put(KEY_PASSWORD, password);
return db.insert(DATABASE_NAME, null, initialValues);
}
//deletes contact
/*public boolean deleteContact(long rowId)
{
return db.delete(DATABASE_NAME, KEY_ROWID + "+" + rowId, null) > 0;
}
//retrieve contacts
public Cursor getAllContacts()
{
return db.query(DATABASE_NAME, new String[]{KEY_ROWID, KEY_NAME,
KEY_PASSWORD}, null, null, null, null, null);
}
//retrieve specified contact
public Cursor getContact(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_NAME, new String[] {KEY_ROWID,
KEY_NAME, KEY_PASSWORD}, KEY_ROWID + "=" + rowId, null,
null, null, null, null);
if(mCursor!= null){
mCursor.moveToFirst();
}
return mCursor;
}
public Boolean updatContact(long rowId, String user, String password)
{
ContentValues args = new ContentValues();
args.put(KEY_USER, user);
args.put(KEY_PASSWORD, password);
return db.update(DATABASE_NAME, args, KEY_ROWID + "=" + rowId, null) > 0;
}*/
}
以下是我的MainActivity,以防万一 包yc.android.yourchallenger;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button insert = (Button) findViewById(R.id.insert_contact);
final String john = "john";
final String pass = "mavsman";
final DBAdapter db = new DBAdapter(this);
db.open();
if(db.open() == null)
{
Toast.makeText(this, "database not open", Toast.LENGTH_LONG).show();
}
db.insertContact(john, pass);
db.close();
insert.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
}
});
}
};
答案 0 :(得分:2)
正如Code-Guru指出的那样,您将数据插入错误表。
但永远不会创建CORRECT表:
本声明:
static final String DATABASE_CREATE = "create table contact " + KEY_USER + KEY_PASSWORD;
将生成错误的命令
"create table contact userpassword"
无法创建表格。
你需要写:
static final String DATABASE_CREATE = "create table contact (" + KEY_USER + " TEXT, " + KEY_PASSWORD + " TEXT)";
将产生右命令:
"create table contact (user TEXT, password TEXT)"
答案 1 :(得分:1)
static final String DATABASE_CREATE =
"create table contact " + KEY_USER + KEY_PASSWORD;
执行时,这将创建一个名为“contact”的表,但您尝试插入名为“YCDB”的表中。后者是数据库的名称,它与该数据库中的表名称非常不同。我建议您在适当的时候创建一个TABLE_NAME
变量。