Android + SQLite

Source: Internet
Author: User

I have mentioned the problem of persistent access to sharedpreference and file operations data in Android Application Development. Today I will introduce the detailed operations of SQLite applications in Android:

In Android Application Development, we may be most curious about how to implement persistent data persistence. Google adopts the shortest and most flexible data access methods, that is, SQLite is a lightweight persistent data access method. The previous sharedpreferences only required the configuration of small data and its parameters, and stored the local files for access, however, we need to manage data like using MySQL or Oracle on a PC terminal. SQLite can also be used. If you did J2EE development before, you may be familiar with MySQL, Oracle, and other database operations. If that is the case, you can operate SQLite again and then ask about the business, the biggest difference between them is that a small one can be directly put into a browser for operations. A complicated local installation and configuration are required, and then managed on the server, here we can know why we should use the SQLite database when developing mobile phones. The following describes the operation steps and possible problems:

When we perform the first operation on the SQLite database, we call two methods: getreadabledatabase (), getwritabledatabase (), of course, when you perform the first operation, the operation class you create must first inherit from sqliteopenhelper. It requires you to implement two methods: oncreate (sqlitedatabase dB ); onupgrade (sqlitedatabae dB, int oledversion, int newversion), you must implement a constructor, that is, the class you create is the opensqlitedemo class, and its constructor is: public opensqlitedemo (context, string name, cursor cursorfactory, int version) {super (context, name, cursorfactory, Version) ;}. You can construct a context object. You can use the context object to operate the method. Therefore, when you call this constructor, a version number is sent and can be determined by the version number, whether or not we need to call the onupgrade () method to update the structure of the data table. Let's take a look at the operations to judge the source code: When we call the getwritabledatabase method in the sqliteopenhelper class, first, there will be a context to save the database object for judgment, that is, mdatabase! = NULL and mdatabase. isopen () and mdatabae. after isreadonly (), if the database has been created, it will be skipped; otherwise, it will directly return the created mdatabae. When the first execution is performed, it will definitely jump over to create a new database, note that another step during this period is that it determines that a non-state exception is thrown when the installation is initialized to flase. It can be explained from the source code statement: If (misinitializing is false) {Throw new illgealstateexception ("getwritabledtabase called recurisively") ;}, then we can judge by the version number. When the version number is set to 0, the default value is 0, in this case, oncreadte (db) is called to create a new database. You can set the database name based on your name in the constructor. If you have created the database, however, when I want to change the structure of the database table or update the version, we can set its version number to a different version number, which is generally set for numbers floating up, you can operate the data table in the onupgrade () method. You can execute the execsql (SQL) method to execute it, for example, adding a column to the table and deleting a column, when we call the getwritabledatabase method of the sqliteopenhelper class, we will determine whether the version number is the same as before. If it is different, we will call the onupgrade (dB, version, mnewversion) method for updates, OK. This is the operation we performed when creating a database. Do not forget. The first operation will judge whether the openorcreatedatabase () method is used to create a database, next we will introduce how to use SQLite to perform curd operations on data. In SQLite, there are also four curd operations, but we are more accustomed to using execsql provided by SQLite (representing an increase, update and delete operations) and rawquery (queries) operations, because there are only two methods that are convenient and flexible to use, but here we can note that, when performing corresponding operations, we 'd better find the current class to construct the context, and then get the information under the class through context. If you want to operate on another class, if you use context to construct the context without constructing the context, a null pointer exception will be thrown. Please note that, in my resources, you have the source code for download and its detailed reference. The curd operation is not detailed here. It is as simple as operating the Oracle database in J2EE, if you have any questions, please download my source code: http://jiangshide.download.csdn.net/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.