Requirements
Basic functional requirements that need to be implemented:
1. Storing student information in the database
2. Display a list of all student information
3. Delete the first piece of information from the database table
Interface production
1. Layout the XML file first
<EditText android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "@string/hint"Android:id= "@+id/shuru"android:textsize= "20sp"/> <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/add"Android:text= "@string/add"android:textsize= "20sp"/> <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/delete"Android:text= "@string/delete"android:textsize= "20sp"/> </LinearLayout> <ListView android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:id= "@+id/list_show"Android:choicemode= "Singlechoice"Android:listselector= "#fff0" > </ListView>
String.xml file
<resources> <string name= "app_name" >SQLwork</string> <string name= "name" > Name </ string> <string name= "hint" > Please enter your name </string> <string name= "Add" > Add a record </string > <string name= "Delete" > Delete first record </string></resources>
2. Create a database table
Public classMydbhelperextendsSqliteopenhelper {PrivateString sql = "CREATE TABLE student (_id integer primary key autoincrement," + "name text NOT null)";//to create a table's SQL statement PublicMydbhelper (Context context) {Super(Context, "Studentinfo",NULL, 1);} @Override Public voidonCreate (Sqlitedatabase db) {db.execsql (SQL);}//newversion>oldversion (old version) call@Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) {Db.execsql ("DROP table if exists student"); OnCreate (db);}
Get Data
Public classNameImplementsSerializable {Private int_id;PrivateString name; PublicName () {} Publicname (String name) { This. name=name;} Public intget_id () {return_id;} Public voidSET_ID (int_id) { This. _id =_id;} PublicString GetName () {returnname;} Public voidsetName (String name) { This. Name =name;}
ways to add and delete data
Public classNamedao {PrivateContext context;//ContextPrivateMydbhelper Helper;//Create, upgrade, open a databasePrivateSqlitedatabase DB;//change the table and additions PublicNamedao (Context context) {Helper=NewMydbhelper (context);} Public voidInsert (name name) {//Open Databasedb=helper.getwritabledatabase (); //Generate Data collectionContentvalues values=Newcontentvalues (); Values.put ("Name", Name.getname ()); //EXECUTE StatementDb.insert ("Student",NULL, values);} PublicCursor SelectAll () {db=helper.getreadabledatabase (); Cursor Cursor= Db.query ("Student",NULL,NULL,NULL,NULL,NULL,NULL); returncursor;} Public voidDeleteintID) {db=helper.getwritabledatabase (); Db.delete ("Student", "_id=?",NewString[]{string.valueof (ID)});//to delete data based on the ID number of the data}}
Delete First record
CaseR.id.delete:if(name!=NULL){//determines if the record is empty, and if it is empty, prompts the record to be empty.Cursor.movetofirst ();//the first line of dataName=NewName (); NAME.SET_ID (Cursor.getint (Cursor.getcolumnindex ("_ID")) ; Gets the ID of the first row of data Namedao.delete (name.get_id ());//call the Delete method to delete the first recordNamedao =NewNamedao ( This); Cursor=Namedao.selectall (); Adapter=NewMycursoradapter ( This, cursor); Lvshow.setadapter (adapter); Toast.maketext ( This, "Delete succeeded", Toast.length_short). Show (); } toast.maketext ( This, "Record is empty", Toast.length_short). Show (); Break;
SQL database Storage Design