Home Database Mysql Tutorial Android中数据库升级

Android中数据库升级

Jun 07, 2016 pm 04:10 PM
android class public upgrade database

public class MySQLiteHelper extends SQLiteOpenHelper { public static final String span style=font-family: Arial, Helvetica, sans-serif;SQL_CREATE/spanspan style=font-family: Arial, Helvetica, sans-serif;= create table news ( /span + _id in

public class MySQLiteHelper extends SQLiteOpenHelper {  
      
    public static final String <span style="font-family: Arial, Helvetica, sans-serif;">SQL_CREATE</span><span style="font-family: Arial, Helvetica, sans-serif;">= "create table news ("  </span>
            + "_id integer primary key autoincrement, "  
            + "nametext, "  
            + "agetext, )";  
  
    public MySQLiteHelper(Context context, String name, CursorFactory factory,  
            int version) {  
        super(context, name, factory, version);  
    }  
  
    @Override  
    public void onCreate(SQLiteDatabase db) {  
        db.execSQL(SQL_CREATE); 
    }  
  
    @Override  
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
    }  
  
} 
Copy after login
当我们如上创建了个SQLiteHelper,我们知道会在本地的目录database中生成了个.db数据库文件! 可是我们遇到版本迭代往往要做更多的事情 这时候的会再创建个新的数据库
Copy after login
这时候我们编译完成之后发现没有在database相对应的目录下生成一个相对应的表。当然我们在调试代码的时候可以清楚应用数据这样重新运行的时候会把onCreate方法再次执行一次那当然就会把未生成成功的表生成出来,可以在用户迭代更新应用的时候总不能每次都清楚数据是吧! 所以:
Copy after login
<pre name="code" class="java">@Override  
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    onCreate(db);  
} 
Copy after login
主要思路就是更新version,就是说你用户现有装的app上version是1的时候,在迭代版本更新的时候要更新version为>1的值 这样我们在new MySQLiteHelper() 的时候就会执行onUpgrade()方法,然后在在该方法中写入相对应的操作就会在database下生产相对应的表而不需要用户去清除应用的数据。
Copy after login
Copy after login
当然这种方法有一个很严重的弊端就是  可能我们在发布版本的时候忘记修改该version导致无法操作数据引起一系列不可收拾的bug。 所以推荐使用LitePal开源库进行对数据库操作!
详情可见  <a target=_blank href="http://http://blog.csdn.net/guolin_blog/article/details/39151617">点击打开链接</a>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:23 PM

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Sep 12, 2024 pm 09:21 PM

Samsung has not offered any hints yet about when it will update its Fan Edition (FE) smartphone series. As it stands, the Galaxy S23 FE remains the company's most recent edition, having been presented at the start of October 2023. However, plenty of

New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:22 PM

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Sep 27, 2024 am 06:23 AM

The Redmi Note 14 Pro Plus is now official as a direct successor to last year'sRedmi Note 13 Pro Plus(curr. $375 on Amazon). As expected, the Redmi Note 14 Pro Plus heads up the Redmi Note 14 series alongside theRedmi Note 14and Redmi Note 14 Pro. Li

Oppo Find X8 design looks like a cross between Apple iPhone 16 Pro and OnePlus Open in early images Oppo Find X8 design looks like a cross between Apple iPhone 16 Pro and OnePlus Open in early images Sep 28, 2024 am 06:04 AM

Historically, Oppo has refreshed its flagship 'Find X' series in late winter or early spring, save for the original Find X that it announced in June 2018. To that end, the Find X7 and Find X7 Ultra are barely more than six months old at this point. H

Galaxy A16 5G: Leak reveals future release aimed at freshening up Samsung\'s cheapest Galaxy A series model Galaxy A16 5G: Leak reveals future release aimed at freshening up Samsung\'s cheapest Galaxy A series model Sep 12, 2024 pm 12:20 PM

It seems that the launch of a new entry-level Galaxy A series smartphone could be in the offing. Although Samsung has not officially announced anything just yet, Android Headlines has already published render images of what it claims is a Galaxy A15

Xiaomi Redmi Note 14, Redmi Note 14 Pro and Redmi Note 14 Pro Plus to shake up mid-range smartphone market with Gorilla Glass Victus 2 and IP69 certification Xiaomi Redmi Note 14, Redmi Note 14 Pro and Redmi Note 14 Pro Plus to shake up mid-range smartphone market with Gorilla Glass Victus 2 and IP69 certification Sep 24, 2024 pm 12:15 PM

Xiaomi has just announced the release date for its next Redmi Note smartphones. As expected, these are set to arrive as three Redmi Note 14 series handsets. More specifically, the company intends to release the Redmi Note 14, Redmi Note 14 Pro and Re

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

See all articles