Java Android/SQLite/多表

Java Android/SQLite/多表,java,android,Java,Android,我正在创建我的第一个android应用程序,在数据库中创建第二个表时遇到了问题 这是我的代码: (DatabaseHelper.java) (DatabaseFunc.java) 公共类数据库func { 静态最终字符串数据库\u NAME=“login.db”; 静态最终int数据库_版本=1; 公共静态final int NAME_COLUMN=1; //TODO:为表中的每列创建公共字段。 //SQL语句创建新数据库。 静态最终字符串数据库\u CREATE=“CREATE table”+

我正在创建我的第一个android应用程序,在数据库中创建第二个表时遇到了问题

这是我的代码:

(DatabaseHelper.java)

(DatabaseFunc.java)

公共类数据库func
{
静态最终字符串数据库\u NAME=“login.db”;
静态最终int数据库_版本=1;
公共静态final int NAME_COLUMN=1;
//TODO:为表中的每列创建公共字段。
//SQL语句创建新数据库。
静态最终字符串数据库\u CREATE=“CREATE table”+“LOGIN”+
(“+”ID“+”整数主键自动递增“+”用户名文本、密码文本、团队ID文本)”;
静态最终字符串数据库\u CREATE2=“创建表”+“SMSREG”+“”+
(“+”ID“+”整型主键自动递增“+”音调文本、消息文本、成功文本)”;
//变量来保存数据库实例
公共数据库数据库;
//使用数据库的应用程序的上下文。
私人最终语境;
//数据库打开/升级帮助程序
私有数据库助手dbHelper;
公共数据库func(上下文_上下文)
{
上下文=_上下文;
dbHelper=newdatabasehelper(上下文、数据库名称、null、数据库版本);
}
public DatabaseFunc open()引发SQLException
{
db=dbHelper.getWritableDatabase();
归还这个;
}
公众假期结束()
{
db.close();
}
公共SQLiteDatabase getDatabaseInstance()
{
返回分贝;
}
public void insertEntry(字符串用户名、字符串密码)
{
ContentValues newValues=新ContentValues();
//为每行指定值。
newValues.put(“用户名”,用户名);
newValues.put(“密码”,PASSWORD);
newValues.put(“TEAMID”,“0”);
//将该行插入表中
db.insert(“登录”,null,newValues);
///Toast.makeText(上下文,“提醒已成功保存”,Toast.LENGTH_LONG.show();
}
public void insertNewSMS(字符串SMSNumber、字符串消息、字符串Success){
ContentValues SMSPut=新ContentValues();
SMSPut.put(“音调”,SMSNumber);
SMSPut.put(“消息”,消息);
SMSPut.put(“成功”,成功);
db.插入(“SMSREG”,空,SMSPut);
}
公共字符串DisplaySMS(){
字符串tempholder=“”;
游标cursor1=db.query(false,“SMSREG”,null,null,null,null,null,null,null);
游标1.moveToFirst();
while(cursor1.isAfterLast()==false)
{
tempholder+=“”+cursor1.getString(cursor1.getColumnIndex(“TONUM”))+”:@FIELDSEP@:“+cursor1.getColumnIndex(cursor1.getColumnIndex(“消息”)+”:@FIELDSEP@:“+cursor1.getString(cursor1.getColumnIndex(“成功”)+”:@ROWSEP:”;
游标1.moveToNext();
}
游标1.close();
返回临时持有人;
}
public int deleteEntry(字符串用户名)
{
//String id=String.valueOf(id);
字符串,其中=“USERNAME=?”;
int numberofentriesdelected=db.delete(“LOGIN”,其中,新字符串[]{UserName});
//Toast.makeText(上下文,“成功删除条目的数量:”+numberofentriesdelected,Toast.LENGTH_LONG).show();
已删除的返回编号;
}   
公共字符串getSinlgeEntry()
{
Cursor Cursor=db.query(false,“LOGIN”,null,null,null,null,null,null,null);
如果(cursor.getCount()尝试更改

static final int DATABASE_VERSION = 1;

我怀疑您在创建第一个表后运行了代码,然后添加了代码来创建第二个表。因为版本号没有更改,它认为它已经创建了版本1,所以不重新运行创建。或者,您可以从设备中删除数据库,然后将其保留在版本1即可


通过增加数据库的版本,您将强制运行
onUpgrade
方法,该方法依次调用
onCreate

我刚刚在实际设备上尝试了您的代码。创建了一个DatabaseFunc实例,名为open().使用根浏览器检查数据库文件,两个表创建正确…这很好,解决了问题-非常感谢。几个小时的头发拉扯和一个整数更改!!我希望我能投票支持你的答案,但我不能,因为我是新来的。这就像很多事情一样,知道怎么做很容易。很高兴能提供帮助。
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);             
        }   
}
static final int DATABASE_VERSION = 1;
static final int DATABASE_VERSION = 2;