为什么说android sqlite数据库不存在列?

时间:2014-02-27 21:49:14

标签: android sqlite

我在应用程序上工作,我创建了一个数据库,但是当我输入数据时,logcat会出现错误,说“列密码不存在”。这是为什么?

这是我的数据库代码(部分代码):

public class DBHandler extends SQLiteOpenHelper {

    private static final int DATABASE_NUMBER = 1;

    private static final String DATABASE_NAME = "Snipeshot";

    private static final String TABLE_USERS = "users";

    private static final String KEY_USERNAME ="username ";
    private static final String KEY_PASSWORD = "password ";

    public DBHandler(Context ctx){
        super(ctx, DATABASE_NAME, null, DATABASE_NUMBER);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String CREATE_USER_TABLE = "CREATE TABLE " + DATABASE_NAME + "(" + KEY_USERNAME + "TEXT, " + KEY_PASSWORD
                + "TEXT" + ");";
        db.execSQL(CREATE_USER_TABLE);
    }

以下是将数据输入数据库的代码:private class register extends

AsyncTask<Object,Void,Boolean>{
        String f,l,d,em,un,pw,cpw;
        boolean success = true;
        protected void onPreExecute(){
            f = fn.getText().toString();
            l = ln.getText().toString();
            d = dob.getText().toString();
            em = e.getText().toString();
            un = u.getText().toString();
            pw = p.getText().toString();
            cpw = cp.getText().toString();
        }

        @Override
        protected Boolean doInBackground(Object... arg0) {
            // TODO Auto-generated method stub
            if(f == null || f.equalsIgnoreCase("")  ){
                success = false;
                return false;
            }

            else if(l == null || l.equalsIgnoreCase("") ){
                success = false;
                return false;
            }

            else if(d == null || d.equalsIgnoreCase(""))
            {
                success = false;
                return false;
            }

            else if(em == null || em.equalsIgnoreCase("")){
                success = false;
                return false;
            }

            else if(un == null || un.equalsIgnoreCase("")){
                success = false;
                return false;
            }

            else if(pw == null || pw.equalsIgnoreCase("")){
                success = false;
                return false;
            }

            else if(!(cpw.equals(pw))){
                success = false;
                return false;
            }

            if(success){
                db.addUSer(new User(un,pw));
            }

            return true;
        }


        protected void onPostExecute(Boolean results){
            results = true;
            ctx.startActivity(new Intent(ctx,SignIn.class));




        }

这是addUSer代码:

public void addUSer(User user) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_USERNAME, user.getuserName()); // 
        values.put(KEY_PASSWORD, user.getPassword()); // 

        // Inserting Row
        db.insert(TABLE_USERS, null, values);
        db.close(); // Closing database connection
    }

3 个答案:

答案 0 :(得分:1)

从上面的代码中,您需要纠正这些错误:

1 - &GT;您的String变量包含尾随空格

private static final String KEY_USERNAME ="username ";
private static final String KEY_PASSWORD = "password ";

纠正于:

private static final String KEY_USERNAME = "username";
private static final String KEY_PASSWORD = "password";

2 - &GT;您应该有一个id列,您可以使用该列唯一地标识每条记录。另外,您编写了DATABASE_NAME而不是TABLE_USERS,并对查询字符串中的间距进行了一些更正。

  

String CREATE_USER_TABLE =“CREATE TABLE”+ DATABASE_NAME +“(”+   KEY_USERNAME +“TEXT,”+ KEY_PASSWORD +“TEXT”+“);”;

纠正于:

创建一个类变量,

private static final String KEY_USERID = "uid";

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USERS + "(" + KEY_USERID + " integer primary key autoincrement, " + KEY_USERNAME + " TEXT, " + KEY_PASSWORD
            + " TEXT" + ");";
    db.execSQL(CREATE_USER_TABLE);
}

不知道这是否有用,但请注意,请告诉我。

答案 1 :(得分:0)

缺少空间:

你必须写“TEXT”,+

而不是

“TEXT” +

答案 2 :(得分:0)

您确实知道您正在呼叫database name而不是table name

private static final String TABLE_USERS = "users";

private static final String KEY_USERNAME ="username ";
private static final String KEY_PASSWORD = "password ";

public DBHandler(Context ctx){
    super(ctx, DATABASE_NAME, null, DATABASE_NUMBER);
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub


 //SHouldn't here TABLE NAME COME
    String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USERS + "(" + KEY_USERNAME + "TEXT, " + KEY_PASSWORD
            + "TEXT" + ");";