An example explains the basic method of android app using its own SQLite database

SQLite database is a database embedded in Android system. It is small and powerful, and can meet the processing of most SQL statements. SQLite database is just a file. Although there are a lot of SQLite, it is not as powerful as MySQL on the PC. moreover, remote database operation through JDBC is not allowed in the Android system, so data can only be obtained through interaction with PHP and servlet through WebService.

The basic sqlitedatabase class represents a database object, which operates and manages the database through sqlitedatabase.

Some basic usage:

Through these static methods, you can easily open and create a new database.

These functions can complete SQL functions. The query results are represented by cursor, which is similar to the resultset class in JDBC. In these classes, the required result rows are obtained through methods move (int offset), movetofirst(), movetolast(), movetonext(), movetoposition (int position) and movetoprivious().

The following is an example to illustrate the basic use of sqlitedatabase:

main.xml:

Mytextview.xml for populating data:

Main.java

Achieved results:

It should be noted that when encapsulating cursor with simplecursoradapter, the column name of the primary key column of the underlying database table is required to be_ ID, because simplecursoradapter can only recognize the primary key column name_ ID table.

Advanced openorcreatedatabase that directly uses sqlitedatabase can directly open or create a new sqlitedatabase, but there is a disadvantage here. Every time the SQL statement is executed, it needs to be carried out in the try catch statement. If the database or table directly operated in the try does not exist, it needs to re create the table in the catch and perform crud operation, and each method related to the database needs to do so. There are too many repeated codes, so sqliteopenhelper class is mostly used in actual development.

Main methods:

usage method:

1) Inherit sqliteopenhelper. The parameter string name in the constructor is the name of the database.

2) Override the oncreate and onupgrade methods.

Mysqliteopenhelper class:

Main.java

Effect achieved:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>