Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android SQLite中的现有表上添加列?_Java_Android_Eclipse_Android Sqlite - Fatal编程技术网

Java 如何在android SQLite中的现有表上添加列?

Java 如何在android SQLite中的现有表上添加列?,java,android,eclipse,android-sqlite,Java,Android,Eclipse,Android Sqlite,如何在现有表登录中添加列。这是我的示例代码 这是我的DataBaseAdapter类: public class DataBaseHelper extends SQLiteOpenHelper { public DataBaseHelper(Context context, String name,CursorFactory factory, int version) { super(context, name, factory, version)

如何在现有表登录中添加列。这是我的示例代码

这是我的DataBaseAdapter类:

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(LoginDataBaseAdapter.DATABASE_CREATE);

    }
    // 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 " + "TEMPLATE");
            // Create a new one.
            onCreate(_db);
    }
这是我的LoginDataBaseAdapter

public class LoginDataBaseAdapter 
{
        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); ";
        // 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  LoginDataBaseAdapter(Context _context) 
        {
            context = _context;
            dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        public  LoginDataBaseAdapter 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);

            // Insert the row into your table
            db.insert("LOGIN", null, newValues);
            ///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
        }
        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(String userName1)
        {
            Cursor cursor=db.query("LOGIN", null, " USERNAME=?", new String[]{userName1}, null, null, null);
            if(cursor.getCount()<1) // UserName Not Exist
            {
                cursor.close();
                return "NOT EXIST";
            }
            cursor.moveToFirst();
            String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
            cursor.close();
            return password;                
        }
        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);

            String where="USERNAME = ?";
            db.update("LOGIN",updatedValues, where, new String[]{userName});               
        }       
}
公共类登录数据库适配器
{
静态最终字符串数据库\u NAME=“login.db”;
静态最终int数据库_版本=1;
公共静态final int NAME_COLUMN=1;
//TODO:为表中的每列创建公共字段。
//SQL语句创建新数据库。
静态最终字符串数据库\u CREATE=“CREATE table”+“LOGIN”+
(“+”ID“+”整数主键自动递增“+”用户名文本、密码文本);”;
//变量来保存数据库实例
公共数据库数据库;
//使用数据库的应用程序的上下文。
私人最终语境;
//数据库打开/升级帮助程序
私有数据库助手dbHelper;
公共登录数据库适配器(上下文_上下文)
{
上下文=_上下文;
dbHelper=newdatabasehelper(上下文、数据库名称、null、数据库版本);
}
public LoginDataBaseAdapter open()引发SQLException
{
db=dbHelper.getWritableDatabase();
归还这个;
}
公众假期结束()
{
db.close();
}
公共SQLiteDatabase getDatabaseInstance()
{
返回分贝;
}
public void insertEntry(字符串用户名、字符串密码)
{
ContentValues newValues=新ContentValues();
//为每行指定值。
newValues.put(“用户名”,用户名);
newValues.put(“密码”,PASSWORD);
//将该行插入表中
db.insert(“登录”,null,newValues);
///Toast.makeText(上下文,“提醒已成功保存”,Toast.LENGTH_LONG.show();
}
public int deleteEntry(字符串用户名)
{
//String id=String.valueOf(id);
字符串,其中=“USERNAME=?”;
int numberofentriesdelected=db.delete(“LOGIN”,其中,新字符串[]{UserName});
//Toast.makeText(上下文,“成功删除条目的数量:”+numberofentriesdelected,Toast.LENGTH_LONG).show();
已删除的返回编号;
}   
公共字符串getSinlgeEntry(字符串用户名1)
{
Cursor Cursor=db.query(“LOGIN”,null,“USERNAME=?”,新字符串[]{userName1},null,null,null);

如果(cursor.getCount()首先必须更新SQLite数据库版本,那么将运行onUpgrade()方法,该方法将删除所有数据。然后,将使用您在数据库中定义的新模式重新生成表

因此,这带来的主要问题是,当表将被删除时,您必须找到一种方法来恢复表中已经存在的数据。在这之前,您确实需要运行onUpgrade,因此使用此方法保存需要从数据库中保存的任何数据

static final int DATABASE_VERSION = 2;

和更新数据库创建字符串。

如果只想添加一个或多个列,您还可以更改现有表。因此,假设您想向名为“my_table”的表添加一个名为“my_new_col”的新列,除了将数据库版本号从1更新为2之外,还可以更新表的架构,而不会丢失y数据

...
@Override
public void onUpgrade( SQLiteDatabase db, int oldVersion, int newVersion ) {
    switch( newVersion ) {

        case 2: /* this is your new version number */

            // ... Add new column 'my_new_col' to table 'my_table'
            db.execSQL( "alter table my_table add column my_new_col" ) ;
            break ;
    }
}

就是这样。当然,您希望在新列上定义其他约束,并确保新列包含在“onCreate(…)中原始表的创建中“.

您的意思是说我会将静态最终int数据库\u VERSION=1;更改为静态最终int数据库\u VERSION=2;然后我会添加如下列..静态最终字符串数据库\u CREATE=“CREATE table”+“LOGIN”+(“+”ID“+”integer primary key autoincrement,“+“用户名文本、密码文本、姓氏文本、姓氏文本、部门文本);";就像那样?是的,但只有当你同意丢失表中的数据时才这样做是的,但它不会更新。相反,我需要更改整个表--当你更改数据库版本时,它不会升级?应该在调用构造函数后立即进行。如果你想更改整个表,那么同样简单,你需要做的就是只需更改您的创建表脚本只是一个说明…您可能需要卸载应用程序并重新安装它。清除应用程序数据只会清空数据库,而不会删除它。数据库升级可能发生在第一次安装更新时,您不会看到它再次被调用。