Android includes SQLite, which is often used in embedded systems, eliminating the developers' ability to port and install on their own. SQLite supports most SQL92 standards, and many commonly used SQL commands can be used on SQLite. In addition, Android also provides a series of custom methods to simplify the operation of SQLite database. However, if you have cross-platform needs, it is recommended to use standard SQL statements, after all, it is easy to port between multiple platforms.
This article mainly explains the basic usage of SQLite, such as: creating a database, querying data tables with SQL commands, inserting data, closing the database, and using GridView to implement a pagination bar (about the usage of GridView) to display data.
The source code of the paging bar is as follows:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:andro android:layout_height="wrap_content" android:paddingBottom="4dip" android:layout_width="fill_parent"> <TextView android:layout_width="wrap_content" android:layout_below="@+id/ItemImage" android:layout_height="wrap_content" android:text="TextView01" android:layout_centerHorizontal="true" android:> </TextView> </RelativeLayout>
The source code is as follows:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android: android:text="Create a database"></Button> <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Insert a string of experimental data" android:></Button> <Button android:layout_height="wrap_content" android: android:text="Close the database" android:layout_width="fill_parent"></Button> <EditText android:text="@+id/EditText01" android: android:layout_width="fill_parent" android:layout_height="256dip"></EditText> <GridView android: android:layout_width="fill_parent" android:layout_height="32dip" android:numColumns="auto_fit" android:columnWidth="40dip"></GridView> </LinearLayout>
The source code of this article is as follows:
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class testSQLite extends Activity { /** Called when the activity is first created. */ Button btnCreateDB, btnInsert, btnClose; EditText edtSQL;//Display paging data SQLiteDatabase db; int id;//The id accumulation mark when adding records must be global static final int PageSize=10;//The total number of data per page when paging private static final String TABLE_NAME = "stu"; private static final String ID = "id"; private static final String NAME = "name"; SimpleAdapter saPageID;// Paging bar adapter ArrayList<HashMap<String, String>> lstPageID;// The data source of the pagination bar is related to PageSize and total data @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); btnCreateDB = (Button) (); (new ClickEvent()); btnInsert = (Button) (); (new ClickEvent()); btnClose = (Button) (); (new ClickEvent()); edtSQL=(EditText)(.EditText01); GridView gridview = (GridView) findViewById();//Pagination bar control // Generate dynamic arrays and transfer data lstPageID = new ArrayList<HashMap<String, String>>(); // Generate the ImageItem of the adapter <====> elements of the dynamic array, the two correspond one by one saPageID = new SimpleAdapter(, // No explanation lstPageID,// Source of data ,//XML implementation new String[] { "ItemText" }, new int[] { }); // Add and display (saPageID); // Add message processing (new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { LoadPage(arg2);//Read the corresponding data according to the selected page } }); } class ClickEvent implements { @Override public void onClick(View v) { if (v == btnCreateDB) { CreateDB(); } else if (v == btnInsert) { InsertRecord(16);//Insert 16 records RefreshPage(); }else if (v == btnClose) { (); } } } /* * Read paged data with the specified ID * SQL:Select * From TABLE_NAME Limit 9 Offset 10; * Indicates that data is obtained from the TABLE_NAME table, skip 10 rows, and take 9 rows */ void LoadPage(int pageID) { String sql= "select * from " + TABLE_NAME + " Limit "+(PageSize)+ " Offset " +(pageID*PageSize); Cursor rec = (sql, null); setTitle("Total number of data for the current page:"+(())); // Get the field name String title = ""; int colCount = (); for (int i = 0; i < colCount; i++) title = title + (i) + " "; // List all data String content=""; int recCount=(); for (int i = 0; i < recCount; i++) {//Locate a data (i); for(int ii=0;ii<colCount;ii++)//Locate to each field in a data { content=content+(ii)+" "; } content=content+"/r/n"; } (title+"/r/n"+content);//Show it (); } /* * Create databases and data tables in memory */ void CreateDB() { // Create a database in memory db = (null); ("DB Path", ()); String amount = (databaseList().length); ("DB amount", amount); // Create a data table String sql = "CREATE TABLE " + TABLE_NAME + " (" + ID + " text not null, " + NAME + " text not null " + ");"; try { ("DROP TABLE IF EXISTS " + TABLE_NAME); (sql); } catch (SQLException e) {} } /* * Insert N pieces of data */ void InsertRecord(int n) { int total = id + n; for (; id < total; id++) { String sql = "insert into " + TABLE_NAME + " (" + ID + ", " + NAME + ") values('" + (id) + "', 'test');"; try { (sql); } catch (SQLException e) { } } } /* * Refresh the page after insertion */ void RefreshPage() { String sql = "select count(*) from " + TABLE_NAME; Cursor rec = (sql, null); (); long recSize=(0);//Get the total number (); int pageNum=(int)(recSize/PageSize) + 1;//Get the page count (); for (int i = 0; i < pageNum; i++) { HashMap<String, String> map = new HashMap<String, String>(); ("ItemText", "No." + (i)); (map); } (); } }
The above is all about this article, I hope it will be helpful to everyone's learning.