Basic Concepts :
SQLite is an open-source, embedded relational database that implements a self-contained, 0 configuration, transactional SQL database engine. It is characterized by its high portability, ease of use, compact structure, high efficiency and reliability.
Sqliteopenhleper (Important Database helper Class):
Sqliteopenhleper is a helper class that inherits from Sqlitedatabase and is used to manage the creation of databases and version updates, and the two main ways to implement this are: OnCreate (Sqlitedatabase db), Onupgrade (sqlitedatabase db,int oldversion,int newversion), respectively, create the database and update the database.
1 PackageCom.example.sqliteData.OpenHelper;2 3 ImportAndroid.content.Context;4 Importandroid.database.sqlite.SQLiteDatabase;5 ImportAndroid.database.sqlite.SQLiteOpenHelper;6 7 /**8 * Database Helper class9 */Ten Public classPersonopenhelperextendsSqliteopenhelper { One /** A * @paramContext - */ - PublicPersonopenhelper (Context context) { the Super(Context, "person.db",NULL, 1); - } - - /** + * When the database library was first created, call OnCreate () - * + * @paramDB The database that was created A */ at @Override - Public voidonCreate (Sqlitedatabase db) { -Db.execsql ("CREATE TABLE person" (ID integer primary key autoincreament,name varchar (), number varchar (20)) "); - } - - @Override in Public voidOnupgrade (Sqlitedatabase sqlitedatabase,intIintI2) { - //I don't usually use it when I'm a beginner to } +}
SQLite additions and deletions : Sqllite provides an API for manipulating databases, and we can manipulate them by instantiating Sqlitedatabase. (insert,query,update)
Add Data Insert (string table, String nullcolumnhack, contentvalues values)
Parameter explanation:
Table name
Nullcolumnhack default values for empty columns are generally written as null
Contentvalues fill in map into row name and column values. Code Demo:
Public void Add (string name, string pwd) { = helper.getwritabledatabase (); contentvalues CV=new contentvalues (); Cv.put ("username", name); Cv.put ("password", pwd); Db.insert ("User",null, CV); // nullcolumnhack default values for empty columns db.close (); }
Deleting Data Delete (string table, String Whereclause, string[] whereargs)
Table name
Whereclause Delete Condition
Whereargs Delete Condition Value Data Group code Demo
Public void Delete (String name) { = helper.getwritabledatabase (); String whereclause= "id=?"; // ? Represents the placeholder string[] whereargs={string.valueof (1)}; Db.delete ("User", Whereclause,whereargs); Db.close (); }
Querying data Query (string table, string[] columns, string selection, string[] Selectionargs, String groupBy, String having, string by, String limit)
Table name
Columns Column Name
Selection conditional clauses, equivalent to where
An array of parameters for Selectionargs conditional words
GroupBy Grouping columns
Having group conditions
Sort by order class
Limit paging Query limits
Complement: Cursor return value equals result set ResultSet code display
PublicList<person>FindAll () {Sqlitedatabase db=helper.getwritabledatabase (); List<Person> persons =NewArraylist<person>(); Cursor Cursor= Db.query ("User",NULL,NULL,NULL,NULL,NULL,NULL,NULL); while(Cursor.movetonext ()) {intid = cursor.getint (cursor.getcolumnindex ("id"))); String name= Cursor.getstring (Cursor.getcolumnindex ("name")); String Number= Cursor.getstring (Cursor.getcolumnindex ("number")); Person P=NewPerson (ID, name, number); Persons.add (P); } cursor.close (); Db.close (); returnpersons; }
Modifying data upgrade(string table, contentvalues values, String whereclause, string[] whereargs)
Table name
Contentvalues key value pair map
Selection conditional clauses, equivalent to where
An array of parameters for Selectionargs conditional words
Sqlitedatabase db = helper.getwritabledatabase (); New contentvalues (); Values.put ("password", "123456"); = "Id=?" ; = {string.valueof (1)}; Db.update ("User", Values, Whereclause, Whereargs); Db.close ();