无法在 Android 上的 SQLite 中创建第二个表

发布于 2024-10-20 18:43:24 字数 8642 浏览 0 评论 0原文

我创建了一个数据库适配器类,它应该创建两个名为 Users_Details 和 Creditcard_Details 的表。然而,当我导出并检查使用 SQLite 浏览器创建的数据库时,我发现只创建了前一个表。我无法理解造成这种情况的原因。 有人可以建议或帮助我调试这个问题吗?

这是数据库适配器的代码:

    package com.androidbook.LoginForm;

    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 {
        /*------------------------User Details ---------------------------------*/
        public static Cursor d;
        public static final String KEY_ROWID = "_id";
        public static final String KEY_Name = "name";
        public static final String KEY_Username = "username";
        public static final String KEY_Password = "password";

        private static final String TAG = "DBAdapter";

        private static final String DATABASE_NAME = "Wallet";
        private static final String DATABASE_TABLE = "User_Details";

        /*------------------------Credit Cards Table----------------------*/

        private static final String KEY_CCID = "_ccid";
        private static final String KEY_CCUNAME= "cuname";
        private static final String KEY_CCNO = "ccno";
        private static final String KEY_CVV  = "cvvno";
        private static final String EXP_DATE = "expdate";
        private static final String CREDITCARDS_TABLE = "Creditcard_Details";

        private static final int DATABASE_VERSION = 1;

        private static final String DATABASE_CREATE =
            "create table"+ DATABASE_TABLE + " ( " + KEY_ROWID + " integer primary key autoincrement, "
            + KEY_Name + " text not null, " + KEY_Username + " text not null, " 
            + KEY_Password + " text not null);";

        private static final String CCTABLE_CREATE = "create table " + 
        CREDITCARDS_TABLE + " (" + KEY_CCID + 
        " integer primary key autoincrement, " +
        KEY_CCUNAME + "text not null, " + KEY_CCNO + " text not null, " + KEY_CVV + " text not null, " + EXP_DATE + " text not null);";
        /*---------------------Create Credit Card Table -------------------------------*/
      /* private static final String CCTABLE_CREATE =
            "create table"+ CREDITCARDS_TABLE + " ( " + KEY_CCID + " integer primary key , "
            + KEY_CCUNAME + " text not null, " + KEY_CCNO + " ccno text not null, " 
            + KEY_CVV + " text not null, " + EXP_DATE + " text not null);";//+ "FOREIGN KEY(_ccid) REFERENCES User_Details(_id))";
        */
        private final Context context;  
        public 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);
                db.execSQL(CCTABLE_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 titles");
                onCreate(db);
            }
        }
        public int Login(String username,String password)
        {
        try
        {
            Cursor c = null;
            c = db.rawQuery("select * from User_Details where username =" + "\""+ username + "\""+" and password="+ "\""+ password + "\"", null);
            c.moveToFirst(); 
            //Toast.makeText(DBAdapter.this, "correct"+" "+c,Toast.LENGTH_LONG).show();
            d= c;//CCview(tempid);
            return c.getCount(); 
        } 
        catch(Exception e) 
        { 
            e.printStackTrace();
            }

        return 0; 
        }


        //-----------------------Display Credit Card -----------------------------

       /* public int Getid(String tempid)
        {   Cursor c;
            c = db.rawQuery("select id from User_Details where username ="
                    + "\""+ username + "\"", null);

            return Integer.parseInt(c.getString(0));

        }*/



        public Cursor CCview(long menuid)throws SQLException 

        {

            Cursor mCursor =
                    db.query(true, CREDITCARDS_TABLE, new String[] {
                            KEY_CCID,
                            KEY_CCUNAME, 
                            KEY_CCNO,
                            KEY_CVV,
                            EXP_DATE,
                            }, 
                            KEY_CCID + "=" + menuid, 
                            null,
                            null, 
                            null, 
                            null, 
                            null);
            if (mCursor != null) {
                mCursor.moveToFirst();
            }
            return mCursor;
        }



        //--------------------Entries into Credit Card Table------------------------------------
        //---insert a title into the database---

        public long insertCreditcard( String j, String k, String l, String date) 
        {


            ContentValues creditValues = new ContentValues();
          //  creditValues.put(KEY_CCID, i);
            creditValues.put(KEY_CCUNAME, j);
            creditValues.put(KEY_CCNO, k);
            creditValues.put(KEY_CVV, l);
            creditValues.put(EXP_DATE, date);
            return db.insert(CREDITCARDS_TABLE, null, creditValues);
        }


      //---opens the database---
        public DBAdapter open() throws SQLException 
        {
            db = DBHelper.getWritableDatabase();

            return this;
        }

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

        //---insert a title into the database---
        public long insertTitle(String name, String username, String password) 
        {
            ContentValues initialValues = new ContentValues();
            initialValues.put(KEY_Name, name);
            initialValues.put(KEY_Username, username);
            initialValues.put(KEY_Password, password);
            return db.insert(DATABASE_TABLE, null, initialValues);
        }

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

        //---retrieves all the titles---
        public Cursor getAllTitles() 
        {
            return db.query(DATABASE_TABLE, new String[] {
                    KEY_ROWID, 
                    KEY_Name,
                    KEY_Username,
                    KEY_Password}, 
                    null, 
                    null, 
                    null, 
                    null, 
                    null);
        }

        //---retrieves a particular title---
        public Cursor getTitle(long rowId) throws SQLException 
        {
            Cursor mCursor =
                    db.query(true, DATABASE_TABLE, new String[] {
                            KEY_ROWID,
                            KEY_Name, 
                            KEY_Username,
                            KEY_Password
                            }, 
                            KEY_ROWID + "=" + rowId, 
                            null,
                            null, 
                            null, 
                            null, 
                            null);
            if (mCursor != null) {
                mCursor.moveToFirst();
            }
            return mCursor;
        }

        //---updates a title---
        public boolean updateTitle(long rowId, String name, 
        String username, String password) 
        {
            ContentValues args = new ContentValues();
            args.put(KEY_Name, name);
            args.put(KEY_Username, username);
            args.put(KEY_Password, password);
            return db.update(DATABASE_TABLE, args, 
                             KEY_ROWID + "=" + rowId, null) > 0;
        }
    }

I have created a DB Adapter class and that is supposed to create two tables called Users_Details and Creditcard_Details. However when I exported and examined my database created with SQLite Browser, I found that only the former table was created. I'm unable to understand as to what's causing this.
Can someone suggest or help me debug this problem?

Here's the code for the DB Adapter:

    package com.androidbook.LoginForm;

    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 {
        /*------------------------User Details ---------------------------------*/
        public static Cursor d;
        public static final String KEY_ROWID = "_id";
        public static final String KEY_Name = "name";
        public static final String KEY_Username = "username";
        public static final String KEY_Password = "password";

        private static final String TAG = "DBAdapter";

        private static final String DATABASE_NAME = "Wallet";
        private static final String DATABASE_TABLE = "User_Details";

        /*------------------------Credit Cards Table----------------------*/

        private static final String KEY_CCID = "_ccid";
        private static final String KEY_CCUNAME= "cuname";
        private static final String KEY_CCNO = "ccno";
        private static final String KEY_CVV  = "cvvno";
        private static final String EXP_DATE = "expdate";
        private static final String CREDITCARDS_TABLE = "Creditcard_Details";

        private static final int DATABASE_VERSION = 1;

        private static final String DATABASE_CREATE =
            "create table"+ DATABASE_TABLE + " ( " + KEY_ROWID + " integer primary key autoincrement, "
            + KEY_Name + " text not null, " + KEY_Username + " text not null, " 
            + KEY_Password + " text not null);";

        private static final String CCTABLE_CREATE = "create table " + 
        CREDITCARDS_TABLE + " (" + KEY_CCID + 
        " integer primary key autoincrement, " +
        KEY_CCUNAME + "text not null, " + KEY_CCNO + " text not null, " + KEY_CVV + " text not null, " + EXP_DATE + " text not null);";
        /*---------------------Create Credit Card Table -------------------------------*/
      /* private static final String CCTABLE_CREATE =
            "create table"+ CREDITCARDS_TABLE + " ( " + KEY_CCID + " integer primary key , "
            + KEY_CCUNAME + " text not null, " + KEY_CCNO + " ccno text not null, " 
            + KEY_CVV + " text not null, " + EXP_DATE + " text not null);";//+ "FOREIGN KEY(_ccid) REFERENCES User_Details(_id))";
        */
        private final Context context;  
        public 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);
                db.execSQL(CCTABLE_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 titles");
                onCreate(db);
            }
        }
        public int Login(String username,String password)
        {
        try
        {
            Cursor c = null;
            c = db.rawQuery("select * from User_Details where username =" + "\""+ username + "\""+" and password="+ "\""+ password + "\"", null);
            c.moveToFirst(); 
            //Toast.makeText(DBAdapter.this, "correct"+" "+c,Toast.LENGTH_LONG).show();
            d= c;//CCview(tempid);
            return c.getCount(); 
        } 
        catch(Exception e) 
        { 
            e.printStackTrace();
            }

        return 0; 
        }


        //-----------------------Display Credit Card -----------------------------

       /* public int Getid(String tempid)
        {   Cursor c;
            c = db.rawQuery("select id from User_Details where username ="
                    + "\""+ username + "\"", null);

            return Integer.parseInt(c.getString(0));

        }*/



        public Cursor CCview(long menuid)throws SQLException 

        {

            Cursor mCursor =
                    db.query(true, CREDITCARDS_TABLE, new String[] {
                            KEY_CCID,
                            KEY_CCUNAME, 
                            KEY_CCNO,
                            KEY_CVV,
                            EXP_DATE,
                            }, 
                            KEY_CCID + "=" + menuid, 
                            null,
                            null, 
                            null, 
                            null, 
                            null);
            if (mCursor != null) {
                mCursor.moveToFirst();
            }
            return mCursor;
        }



        //--------------------Entries into Credit Card Table------------------------------------
        //---insert a title into the database---

        public long insertCreditcard( String j, String k, String l, String date) 
        {


            ContentValues creditValues = new ContentValues();
          //  creditValues.put(KEY_CCID, i);
            creditValues.put(KEY_CCUNAME, j);
            creditValues.put(KEY_CCNO, k);
            creditValues.put(KEY_CVV, l);
            creditValues.put(EXP_DATE, date);
            return db.insert(CREDITCARDS_TABLE, null, creditValues);
        }


      //---opens the database---
        public DBAdapter open() throws SQLException 
        {
            db = DBHelper.getWritableDatabase();

            return this;
        }

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

        //---insert a title into the database---
        public long insertTitle(String name, String username, String password) 
        {
            ContentValues initialValues = new ContentValues();
            initialValues.put(KEY_Name, name);
            initialValues.put(KEY_Username, username);
            initialValues.put(KEY_Password, password);
            return db.insert(DATABASE_TABLE, null, initialValues);
        }

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

        //---retrieves all the titles---
        public Cursor getAllTitles() 
        {
            return db.query(DATABASE_TABLE, new String[] {
                    KEY_ROWID, 
                    KEY_Name,
                    KEY_Username,
                    KEY_Password}, 
                    null, 
                    null, 
                    null, 
                    null, 
                    null);
        }

        //---retrieves a particular title---
        public Cursor getTitle(long rowId) throws SQLException 
        {
            Cursor mCursor =
                    db.query(true, DATABASE_TABLE, new String[] {
                            KEY_ROWID,
                            KEY_Name, 
                            KEY_Username,
                            KEY_Password
                            }, 
                            KEY_ROWID + "=" + rowId, 
                            null,
                            null, 
                            null, 
                            null, 
                            null);
            if (mCursor != null) {
                mCursor.moveToFirst();
            }
            return mCursor;
        }

        //---updates a title---
        public boolean updateTitle(long rowId, String name, 
        String username, String password) 
        {
            ContentValues args = new ContentValues();
            args.put(KEY_Name, name);
            args.put(KEY_Username, username);
            args.put(KEY_Password, password);
            return db.update(DATABASE_TABLE, args, 
                             KEY_ROWID + "=" + rowId, null) > 0;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

已下线请稍等 2024-10-27 18:43:24

尝试更改 DATABASE_VERSION 数字。我建议这样做是因为您在开发过程中可能有一个表(并且 db_ver 为 1),然后在代码中添加了第二个表,但它没有出现(因此这篇文章)。

或卸载+重新安装即可实现此目的。

try changing the DATABASE_VERSION number. I suggest this because you probably during development had one table (and db_ver was 1), then added a 2nd table to the code but it didn't appear (and hence this post).

or uninstall + reinstall would achieve this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文