安卓/ SQLite的/多个表 [英] Android / SQLite / Multiple Tables

查看:406
本文介绍了安卓/ SQLite的/多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建我的第一个Android应用程序,而我在与我的数据库创建第二个表的问题。

I am creating my first android application, and I'm having issues with creating a 2nd table in my database.

这是我的code:

(DatabaseHelper.java)

(DatabaseHelper.java)

public class DataBaseHelper extends SQLiteOpenHelper {
    public DataBaseHelper(Context context, String name,CursorFactory factory, int version) 
    {
               super(context, name, factory, version);
    }
    // Called when no database exists in disk and the helper class needs
    // to create a new one.
    @Override
    public void onCreate(SQLiteDatabase _db) 
    {
            _db.execSQL(DatabaseFunc.DATABASE_CREATE);
            _db.execSQL(DatabaseFunc.DATABASE_CREATE2);

    }
    // Called when there is a database version mismatch meaning that the version
    // of the database on disk needs to be upgraded to the current version.
    @Override
    public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) 
    {
            // Log the version upgrade.
            Log.w("TaskDBAdapter", "Upgrading from version " +_oldVersion + " to " +_newVersion + ", which will destroy all old data");

            // Upgrade the existing database to conform to the new version. Multiple
            // previous versions can be handled by comparing _oldVersion and _newVersion
            // values.
            // The simplest case is to drop the old table and create a new one.
            _db.execSQL("DROP TABLE IF EXISTS " + "LOGIN");
            _db.execSQL("DROP TABLE IF EXISTS " + "SMSREG");
            // Create a new one.
            onCreate(_db);
    }

}

(DatabaseFunc.java)

(DatabaseFunc.java)

public class DatabaseFunc 
{
        static final String DATABASE_NAME = "login.db";
        static final int DATABASE_VERSION = 1;
        public static final int NAME_COLUMN = 1;
        // TODO: Create public field for each column in your table.
        // SQL Statement to create a new database.
        static final String DATABASE_CREATE = "create table "+"LOGIN"+
                                     "( " +"ID"+" integer primary key autoincrement,"+ "USERNAME  text,PASSWORD text, TEAMID text) ";
        static final String DATABASE_CREATE2 = "create table "+"SMSREG"+"" +
                "( " +"ID"+" integer primary key autoincrement,"+ "TONUM  text, MESSAGE text, SUCCESS text) ";
        // Variable to hold the database instance
        public  SQLiteDatabase db;
        // Context of the application using the database.
        private final Context context;
        // Database open/upgrade helper
        private DataBaseHelper dbHelper;
        public  DatabaseFunc(Context _context) 
        {
            context = _context;
            dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        public  DatabaseFunc open() throws SQLException 
        {
            db = dbHelper.getWritableDatabase();
            return this;
        }
        public void close() 
        {
            db.close();
        }

        public  SQLiteDatabase getDatabaseInstance()
        {
            return db;
        }

        public void insertEntry(String userName,String password)
        {
           ContentValues newValues = new ContentValues();
            // Assign values for each row.
            newValues.put("USERNAME", userName);
            newValues.put("PASSWORD",password);
            newValues.put("TEAMID", "0");

            // Insert the row into your table
            db.insert("LOGIN", null, newValues);
            ///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
        }

        public void insertNewSMS(String SMSNumber, String Message, String Success) {
            ContentValues SMSPut = new ContentValues();
            SMSPut.put("TONUM", SMSNumber);
            SMSPut.put("MESSAGE", Message);
            SMSPut.put("SUCCESS", Success);
            db.insert("SMSREG", null, SMSPut);
        }

        public String DisplaySMS() {

            String tempholder = "";
            Cursor cursor1=db.query(false, "SMSREG", null, null, null, null, null, null, null);
            cursor1.moveToFirst();
            while (cursor1.isAfterLast() == false) 
            {
                tempholder += ""+ cursor1.getString(cursor1.getColumnIndex("TONUM")) + ":@FIELDSEP@:" + cursor1.getString(cursor1.getColumnIndex("MESSAGE")) + ":@FIELDSEP@:" + cursor1.getString(cursor1.getColumnIndex("SUCCESS")) + ":@ROWSEP@:";
                cursor1.moveToNext();
            }

            cursor1.close();
            return tempholder;
        }
        public int deleteEntry(String UserName)
        {
            //String id=String.valueOf(ID);
            String where="USERNAME=?";
            int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{UserName}) ;
           // Toast.makeText(context, "Number fo Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
            return numberOFEntriesDeleted;
        }   
        public String getSinlgeEntry()
        {
            Cursor cursor=db.query(false, "LOGIN", null, null, null, null, null, null, null);
            if(cursor.getCount()<1) // UserName Not Exist
            {
                cursor.close();
                return "NOT EXIST";
            }
            cursor.moveToFirst();
            String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
            String username= cursor.getString(cursor.getColumnIndex("USERNAME"));
            String teamids = cursor.getString(cursor.getColumnIndex("TEAMID"));
            cursor.close();
            return username + ":@SEP@:" + password + ":@SEP@:" + teamids;               
        }
        public void  updateEntry(String userName,String password)
        {
            // Define the updated row content.
            ContentValues updatedValues = new ContentValues();
            // Assign values for each row.
            updatedValues.put("USERNAME", userName);
            updatedValues.put("PASSWORD",password);
            updatedValues.put("TEAMID", "0");

            String where="USERNAME = ?";
            db.update("LOGIN",updatedValues, null, null);              
        }   

        public void  updateTeamID(String TeamID)
        {
            // Define the updated row content.
            ContentValues updatedValuesa = new ContentValues();
            // Assign values for each row.
            updatedValuesa.put("TEAMID", TeamID);
            db.update("LOGIN",updatedValuesa, null, null);             
        }   
}

在登录表工作正常,它创建,数据被投入就OK了,它的提取确定,但根据对仿真器可用SQLite的编辑,SMSREG表永远不会得到创建...希望有人能为提醒我一直在拉头发了几个小时! :)

The Login table works fine, it creates, data gets put into it ok, and it's extracted OK, but according to the SQLite editor available for the emulator, the SMSREG table is never getting created... Hopefully someone can advise as I've been pulling hairs out for hours!! :)

我试着添加和删除分号在SQL语句的结束,但似乎没有有所作为。

I've tried adding and remove the semi-colon at the end of the SQL Statements but that doesn't seem to make a difference.

您在先进的帮助将是非常美联社preciated。

Your help in advanced would be very much appreciated.

推荐答案

试着改变

static final int DATABASE_VERSION = 1;

static final int DATABASE_VERSION = 2;

我怀疑你跑了code后,您刚才创建的第一个表,然后添加了code创建第二。因为版本号没有改变,就认为它已经创建的版本1,所以不重新运行的创建。另外,您可以从设备中删除的数据库,然后将其保持在第1版将是美好的。

I suspect you ran the code after you created just the first table and then added the code to create the second. Because the version number hasn't changed, it thinks it's already created version 1, so doesn't re-run the creation. Alternatively you could delete the database from the device and then keeping it at version 1 would be OK.

通过递增DATABASE_VERSION,你会强制 onUpgrade 方法来运行这反过来又调用的onCreate

By incrementing the DATABASE_VERSION, you'll force the onUpgrade method to run which in turn calls the onCreate.

这篇关于安卓/ SQLite的/多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆