Android-SQLite for data storage (saving Login User Information)

Source: Internet
Author: User

Since I am not good at summarization and writing, many concepts are based on online reference learning. I will integrate this knowledge into a specific exercise to understand, consolidate, and share it.

The SQLite I want to learn today is similar to the SQL server I used previously. The basic things are similar. The basic statements for creating database tables and adding, deleting, modifying, and querying data are similar, if you know SQL Server, learning SQLite should be easy to use. The only thing I don't get used to is that when I first learned about SQLite and looked at other people's examples on the Internet, I always felt quite confused. In fact, the reason is that I also implemented a data addition, deletion, modification, and query, there are several different methods. For example, insert data: Use sqlitedatabaseobject to execute SQL db.exe csql (SQL); or DB. insert (table, nullcolumnhack, values); content provide and content resolver are also used in combination (the database provides external access and external data access ).

If the database in an application does not require external access, implement a Database Help class inherited from sqliteopenhelper to support database creation and version update, which cannot be implemented by sqlitedatabase. however, sqlitedatabase has some very important methods for database operations. It is used to create, delete, and query data tables.

Run the add, delete, and modify operations: db.exe csql (SQL); or DB. insert (), DB. delete (), DB. update (), including creating and deleting data tables, can also be implemented through execsql

 

Code

 // Create a table
Public Boolean createtable (){
Sqlitedatabase DB = dbhelper. getwritabledatabase ();
String SQL = "CREATE TABLE if not exists" + table_name + "(ID integer primary key, name varchar, age integer )";
Try {
Db.exe csql (SQL );
Return true;
} Catch (sqlexception ex ){
Log. D (TAG, "create table failure ");
Return false;
}
}

// Add data
Public Boolean adddata (){
String name = etname. gettext (). tostring ();
String age = etage. gettext (). tostring ();
Sqlitedatabase DB = dbhelper. getwritabledatabase ();
String SQL = "insert into" + table_name + "(name, age) values ('" + name + "', '" + age + "')";
Try {
Db.exe csql (SQL );

Return true;
} Catch (sqlexception ex ){
Log. D (TAG, "add data failure ");
Return false;
}
}

// Modify
Public Boolean updatedata (){
Sqlitedatabase DB = dbhelper. getwritabledatabase ();
String SQL = "Update" + table_name + "set age = '2' where name like 'cb '";
Object [] bindargs = {"CB "};
Try {
Db.exe csql (SQL, bindargs );
Return true;
} Catch (sqlexception ex ){
Log. D (TAG, "update data failure ");
Return false;
}

}

 

 

Run the following data query method: DB. rawquery (SQL, selectionargs); or db. Query (table, columns, selection, selectionargs, groupby, having, orderby );

 

Code

 // Query
Public void selectdata (){
Sqlitedatabase DB = dbhelper. getreadabledatabase ();
String [] Columns = {"name "};
Cursor cursor = dB. Query (table_name, columns, null );
String names = "";
While (cursor. movetonext ()){
Int c = cursor. getcolumnindexorthrow ("name ");
String name = cursor. getstring (C );
// <=>
// String name = cursor. getstring (0); // only one column is queried.
If (names = ""){
Names = Name;
} Else {
Names = names + "\ n" + name;
}
}
Tvname. settext (names );
// Another query method
// String SQL = "Select name from" + table_name;
// Curosr cursor = dB. rawquery (SQL, null );
}

 

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.