Sqliteopenhelper is an abstract class that must write a class to inherit it to use it. Sqliteopenhelper has two abstract methods OnCreate () and Onupgrade (), and we want to override both methods in the class to implement creating and updating the database.
The Sqliteopenhelper class also has two important examples of methods Getreadabledatabase and Getwritabledatabase (), both of which can open or create a database. The difference is that when the database is not writable, such as when disk space is full, the Getreadabledatabase () method opens the database in read-only form, and the Getwritabledatabase () method has an exception.
Overriding the two constructor methods of the Sqliteopenhelper class typically use the one with fewer parameters, which takes four arguments, the first parameter is the context context, the second parameter is the table name, and the third parameter allows us to return a custom cursor when querying the data. Typically, null is passed in. The fourth parameter is the version number of the current database, which can be used to upgrade the database.
After building an instance of Sqliteopenhelper, call its getreadabledatabase () or getwritabledatabase () to create the database.
eg
Create a database named Mydatabase.db, creating a new user table with the ID (primary key) and the Name,password property.
The CREATE TABLE statement for the user table is as follows:
CREATE TABLE user{
ID Integer PRIMARY KEY AUTOINCREMENT,//INTEGR: Shaping PRIMARY key: Primary key, Unique key autoincrement: self-growth
Name Text,//text: text type, plus real floating point, blob binary type
Password text)
Execute this SQL statement in your code, complete as follows:
public class Mydatabasehelper extends Sqliteopenhelper {public static final String create_user = "CREATE table Uer (" + "ID Integer PRIMARY key AutoIncrement, "+" Name text, "+" password text, ")";p rivate Context mcontext;public mydatabasehelper (context context, String name, cursorfactoryfactory, int version) {Super (context, name, Factory, version); mcontext = context;} @Overridepublic void OnCreate (Sqlitedatabase db) {db.execsql (create_user); Toast.maketext (Mcontext, "Create succeeded", Toast.length_short). Show (); @Overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {}}
Simple application of "Android" database--Create Database