0

我正在创建一个接受用户信息进行注册的应用程序,但我收到一条错误消息,提示“没有这样的表:UserAccount”,下面是我的代码:

我的数据库助手

public class DBAdapter 
{

    public static final String KEY_ROWID = "_id";
    public static final String KEY_TITLE = "title";
    public static final String KEY_FULLNAME = "fullname";
    public static final String KEY_EMAIL = "email";
    public static final String KEY_PASS = "password";   
    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "pioneer.db";
    private static final int DATABASE_VERSION = 1;
    public static final String DATABASE_TABLE_NAME = "UserAccount";

    private static final String DATABASE_CREATE =
        "create table IF NOT EXISTS users (_id integer primary key autoincrement, "
        + "title text, fullname text, email text, password text);";

    private final Context context; 

    private DatabaseHelper DBHelper;

    private 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) 
        {
            db.execSQL(DATABASE_CREATE);
        }

        @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 users");
            onCreate(db);
        }
    }    

    //---opens the database---
    public DBAdapter opendb() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

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

    //---insert a title into the database---
    public long insertUser(String title, String fullname,String email, String Password) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_TITLE, title);
        initialValues.put(KEY_FULLNAME, fullname);
        initialValues.put(KEY_EMAIL, email);
        initialValues.put(KEY_PASS, Password);
        return db.insert(DATABASE_TABLE_NAME, null, initialValues);
    }

    //---deletes a particular title---
    public boolean deleteTitle(long rowId) 
    {
        return db.delete(DATABASE_TABLE_NAME, KEY_ROWID + 
          "=" + rowId, null) > 0;
    }

    //---retrieves all the titles---
    public Cursor getAllUsers() 
    {
        return db.query(DATABASE_TABLE_NAME, new String[] {
          KEY_ROWID, 
          KEY_TITLE,
          KEY_FULLNAME,
          KEY_EMAIL,
          KEY_PASS,
          }, 
        null, 
        null, 
        null, 
        null, 
        null);
    }

    //---retrieves a particular user---
    public Cursor getUser(long rowId) throws SQLException 
    {
        Cursor mCursor =
                db.query(DATABASE_TABLE_NAME, new String[] {
                          KEY_ROWID, 
                          KEY_TITLE,
                          KEY_FULLNAME,
                          KEY_EMAIL,
                          KEY_PASS,
                          }, 
                  KEY_ROWID + "=" + rowId, 
                  null,
                  null, 
                  null, 
                  null, 
                  null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    //---updates a title---
    public boolean updateTitle(long rowId, String title, String fullname,String email, String Password)
    {
        ContentValues args = new ContentValues();
        args.put(KEY_TITLE, title);
        args.put(KEY_FULLNAME, fullname);
        args.put(KEY_EMAIL, email);
        args.put(KEY_PASS, Password);


        return db.update(DATABASE_TABLE_NAME, args, 
                         KEY_ROWID + "=" + rowId, null) > 0;
    }
}

这就是我使用它的地方

public class RegisterActivity extends Activity {
    Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        final EditText full_Name = (EditText)findViewById(R.id.reg_fullname);
        final EditText mMail = (EditText)findViewById(R.id.reg_email);
        final EditText mPass = (EditText)findViewById(R.id.reg_password);
        final TextView loginScreen = (TextView) findViewById(R.id.link_to_login);
        btn = (Button)findViewById(R.id.btnRegister);

        final DBAdapter db = new DBAdapter(this);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (db !=null){
                db.opendb();
                long uid =db.insertUser("Mr", full_Name.getText().toString(), mMail.getText().toString(), mPass.getText().toString());

                Cursor c = db.getUser(uid);
                if (c !=null){
                    ShowMessage(c.getString(2));
                }
                }
                db.close();


            }

        });
        }

        private void ShowMessage(String message){
            Toast.makeText(this, message, Toast.LENGTH_LONG).show();
        }

        // Listening to Login Screen link
     /*   loginScreen.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                                // Closing registration screen
                // Switching to Login Screen/closing register screen
                finish();
            }
        });*/

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_register, menu);
        return true;
    }

}

任何帮助将不胜感激。

4

5 回答 5

2

您正在创建一个名称 ( users) 与查询语句 () 中使用的名称不同的表UserAccount

public static final String DATABASE_TABLE_NAME = "UserAccount";

private static final String DATABASE_CREATE =
    "create table IF NOT EXISTS users (_id integer primary key autoincrement, "
    + "title text, fullname text, email text, password text);";

它应该是:

private static final String DATABASE_CREATE =
    "create table IF NOT EXISTS "+ DATABASE_TABLE_NAME+" (_id integer primary key autoincrement, "
    + "title text, fullname text, email text, password text);";
于 2012-12-13T10:53:29.160 回答
1
public static final String DATABASE_TABLE_NAME = "UserAccount";
...
"create table IF NOT EXISTS users 

users并且UserAccount是不同的。您应该使它们相同才能正常工作。

于 2012-12-13T10:53:27.977 回答
1

用户!=用户帐户。

您创建表用户,但您在 UserAccounts 中进行查询。

于 2012-12-13T10:54:58.580 回答
1

您的 DATABASE_CREATE 使“用户”表不是 DATABASE_TABLE_NAME 使用的“用户帐户”表。将变量添加到 sql 表创建字符串。

于 2012-12-13T10:57:48.997 回答
0

将创建查询更改为

private static final String DATABASE_CREATE =
        "create table IF NOT EXISTS "+ DATABASE_TABLE_NAME+" (_id integer primary key autoincrement, "
        + "title text, fullname text, email text, password text);";
于 2012-12-13T10:53:20.410 回答