简体   繁体   English

android sqliteDatabase表重新创建

[英]android sqliteDatabase table recreation

I have created one database and one table user in that while running my android app. 在运行我的android应用程序时,我已经在其中创建了一个database和一个表user

Problem is when at the first time i created user table, i miss the field password to create. 问题是,当我第一次创建user表时,我错过了要创建的字段password So i rewrite my code so as to create the user table again which contains the field password in it. 因此,我重写了我的代码,以便再次创建user表,其中包含字段password

But the problem is i had put the code in onCreate method inside my DatabaseHelper class. 但是问题是我已将代码放在DatabaseHelper类中的onCreate方法中。

Since the database is already exists, the function onCreate will never call. 由于数据库已经存在,因此函数onCreate将永远不会调用。

Can anyone tell how can i solve this issue.? 谁能告诉我如何解决这个问题。

You can use onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) method DatabaseHelper class 您可以使用onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)方法DatabaseHelper类

db - The database. db-数据库。

oldVersion - The old database version. oldVersion-旧的数据库版本。

newVersion - The new database version. newVersion-新的数据库版本。

Called when the database needs to be upgraded. 在需要升级数据库时调用。 The implementation should use this method to drop tables, add tables, or do anything else it needs to upgrade to the new schema version. 实现应使用此方法删除表,添加表或执行其他任何升级到新架构版本所需的操作。 The SQLite ALTER TABLE documentation can be found here . 可以在这里找到SQLite ALTER TABLE文档。 If you add new columns you can use ALTER TABLE to insert them into a live table. 如果添加新列,则可以使用ALTER TABLE将它们插入活动表中。 If you rename or remove columns you can use ALTER TABLE to rename the old table, then create the new table and then populate the new table with the contents of the old table. 如果重命名或删除列,则可以使用ALTER TABLE重命名旧表,然后创建新表,然后使用旧表的内容填充新表。 Parameters 参量

few possibilities are available: 几乎没有可用的方法:

  1. if you have root access on your device or using an emulator - you can use adb shell to find your SQLite database and simply drop it (so it will re-created on next time your app starts) or using sqlite command line tool - modify the table structure yourself. 如果您具有设备的超级用户访问权限或使用仿真器-可以使用adb shell查找SQLite数据库并将其删除(这样它将在下次启动应用程序时重新创建它)或使用sqlite命令行工具 -修改表结构自己。 Of course this fits only if you missed something during development and willing quick-fix it fast, not thinking about any existing users. 当然,仅当您在开发过程中错过了某些东西并愿意快速修复它而不考虑任何现有用户时,这才适合。

  2. increase the version number of your application and implement onUpgrade method, where you can do all required transformations on already existing database. 增加应用程序的版本号并实现onUpgrade方法,您可以在该方法上对现有数据库进行所有必需的转换。 This should be used for real application version upgrades, thinking about existing users and their application installations. 这应该用于实际的应用程序版本升级,考虑现有用户及其应用程序安装。

An easy option is just to uninstall the application on the device in the settings menu (or use the adb uninstall command), then the database will also be deleted and recreated if you run the application again. 一个简单的选择就是在设置菜单上卸载设备上的应用程序(或使用adb卸载命令),然后如果再次运行该应用程序,该数据库也将被删除并重新创建。

Quick and simple without having to implement onUpgrade or something, that would be for when the application has hit the market/is really in use. 快速而简单,而无需实施onUpgrade之类的东西,这将在应用程序投放市场时/确实在使用中。

you can simply access your sqlite db using adb shell, you will need root access if your app is installed on the phone. 您只需使用adb shell访问sqlite数据库,如果您的应用已安装在手机上,则需要root访问权限。 Here is some reference - look for Examining sqlite3 Databases from a Remote Shell 这里是一些参考-寻找从远程Shell检查sqlite3数据库

Other way is to remove you database from Settings->Applications->Manage Applications->Your App and hit the Clear Data Button, that will remove your Sqlite db AND your Shared Preferences. 另一种方法是从“设置”->“应用程序”->“管理应用程序”->“您的应用程序”中删除数据库,然后单击“清除数据”按钮,这将删除您的Sqlite数据库和“共享首选项”。 After this you can rerun your app again using the correct table DDL and your Database will be created with the correct definition. 之后,您可以使用正确的表DDL重新运行您的应用程序,并且将使用正确的定义创建数据库。

As programmatically speaking you will need to override the onOpen from your helper and put your table alteration DDL using db.execSql(). 以编程方式来讲,您将需要从助手中覆盖onOpen并使用db.execSql()将表更改DDL放入。 Put your alter table there or alternatively drop your table first and create the new one with the correct definition. 将您的alter table放在此处,或者将其放到最前面,然后使用正确的定义创建新的table。 Here is the class' reference SQLiteOpenHelper and SQLiteDatabase and please also refer to here 这是该类的参考SQLiteOpenHelperSQLiteDatabase ,也请参考此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM