SQLite数据库错误:找不到Android表

时间:2013-02-22 20:44:24

标签: java android database

尝试创建数据库时出现此错误: 错误信息     在此处输入代码     02-23 02:08:46.780:E / Database(32721):错误     插入电子邮件= af contact = 38 dob = 1/8/68 name = ex     02-23 02:08:46.780:E / Database(32721):     android.database.sqlite.SQLiteException:没有这样的表:      配置文件:,同时编译:INSERT INTO配置文件      (email,contact,dob,name)VALUES(?,?,?,?);     02-23 02:08:46.780:E / Database(32721):     atandroid.database.sqlite.SQLiteCompiledSql.native_compile(原生方法)     02-23 02:08:46.780:E / Database(32721):at

package a.vaccination;

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class datahandler {

public static final String KEY_ROWID = "id";
public static final String KEY_name = "name";
public static final String KEY_dob = "dob";
public static final String KEY_contact = "contact";
public static final String KEY_email = "email";
private static final String TAG = "DBAdapter";

private static final String DATABASE_NAME = "profile";
private static final String DATABASE_TABLE = "profiles";
private static final int DATABASE_VERSION = 2;

private static final String DATABASE_CREATE
 = "create table  (id integer primary      keyautoincrement, "
 + "name VARCHAR not null, dob date, contact VARCHAR, email VARCHAR);";
private final Context context;

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public datahandler(Context ctx) {
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

protected static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        try {
            db.execSQL(DATABASE_CREATE);
            //db = DBHelper.getWritableDatabase();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        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 the database---
public datahandler open() throws SQLException {
    db = DBHelper.getWritableDatabase();
    return this;
}

// ---closes the database---
public void close() {
    DBHelper.close();
}

// ---insert a record into the database---
public long insertRecord(String name, String dob, String contact,
        String email) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_name, name);
    initialValues.put(KEY_dob, dob);
    initialValues.put(KEY_contact, contact);
    initialValues.put(KEY_email, email);
    return db.insert(DATABASE_TABLE, null, initialValues);
}

// ---updates a record---
public boolean updateRecord(long rowId, String name, String dob,
        String contact, String email) {
    ContentValues args = new ContentValues();
    args.put(KEY_name, name);
    args.put(KEY_dob, dob);
    args.put(KEY_contact, contact);
    args.put(KEY_email, email);
    return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}

2 个答案:

答案 0 :(得分:1)

您的查询是不正确的:

  • 您缺少tablename
  • 您在密钥和自动增量之间缺少空格

以下更正:

String DATABASE_CREATE = "create table TableName(id integer primary key autoincrement, "
+ "name VARCHAR not null, dob date, contact VARCHAR, email VARCHAR)";

现在它会起作用。

答案 1 :(得分:0)

请在此处尝试使用表格名称

private static final String DATABASE_CREATE
 = "create table TABLENAME (id integer primary key autoincrement, "
 + "name VARCHAR not null, dob date, contact VARCHAR, email VARCHAR);";

这看起来是

背后的原因
  

没有这样的表:个人资料