Source: http://www.bcmeng.com/windows-phone-sqlite1/
This small dream will share with you WP8.1 the basic operation of the SQLite database: (finally there is the whole example of source code) (hope to be able to support the small dream through the site ads, thank you!)
- Set up a database
- Add data
- Delete data
- Change data
- Querying data
(Note: In order for each operation to be displayed on the UI in a timely manner, data binding is done.) data binding will be explained in the following article, first give the data class note, representing a note. Contains the name and content properties. The code is as follows: If not clear, I will explain later:
namespacesqlite{ Public classnote:inotifypropertychanged {Private intID; [AutoIncrement, PrimaryKey] Public intID {Get{returnID;} Set { if(value!=ID) {ID=value; raisePropertyChanged ("ID"); } } } Private stringname; [MaxLength ( -)] Public stringName {Get{returnname;} Set { if(Value! =name) {Name=value; raisePropertyChanged ("Name"); } } } Private stringcontent; [MaxLength ( -)] Public stringContent {Get{returncontent;} Set { if(Value! =content) {Content=value; raisePropertyChanged ("Content"); } } } Public EventPropertyChangedEventHandler propertychanged; protected voidraisePropertyChanged (stringPropertyName) { if(PropertyChanged! =NULL) {propertychanged ( This,NewPropertyChangedEventArgs (PropertyName)); } } }}
Windows Phone 8.1 Development sqlite-Database creation
Privatesqliteasyncconnection Getconn () {return NewSqliteasyncconnection (ApplicationData.Current.LocalFolder.Path +"\\note.db"); } Private Async voidCreatebutton_click (Objectsender, RoutedEventArgs e) {Sqliteasyncconnection conn=Getconn (); awaitConn. Createtableasync<note>(); await NewMessagedialog ("The database was created successfully! "). Showasync (); }
Windows Phone 8.1 Development sqlite-Add data
Sqliteasyncconnection conn =Getconn (); awaitConn. Insertasync (NewNote {Name ="Little Dream", Content ="Little Dream I miss you" }); awaitConn. Insertasync (NewNote {Name ="Little Dream", Content ="Little Dream I love you" }); awaitConn. Insertasync (NewNote {Name ="Little Dream", Content ="Little Dream I like you" }); awaitConn. Insertasync (NewNote {Name ="Little Dream", Content ="Little Dream I hate you" }); awaitConn. Insertasync (NewNote {Name ="Little Dream", Content ="Little Dream I hit you" }); List<Note> notelist =awaitConn. Table<note>(). Tolistasync (); Notes=NewObservablecollection<note>(notelist); Listbox.itemssource=notes; await NewMessagedialog ("Increase data Success! "). Showasync ();
Windows Phone 8.1 development sqlite-Deleting data
Sqliteasyncconnection conn =Getconn (); varquery =awaitConn. Table<note>(). Firstasync (); for(inti =0; I < notes. count;i++ ) { if(Notes[i].id = =query.id) {notes. RemoveAt (i); Break; } } awaitConn. Deleteasync (Query asObject); Listbox.itemssource=notes; await NewMessagedialog ("Delete data Successfully! "). Showasync ();
Windows Phone 8.1 development sqlite-modifying data
Sqliteasyncconnection conn =Getconn (); for(inti =0; I < notes. Count; i++) { if(Notes[i]. name=="Little Dream") {Notes[i]. Content="Little Dream I love you"; } } varquery = conn. Table<note> (). Where (x = x.name=="Little Dream"); varresult =awaitquery. Tolistasync (); foreach(varIteminchresult) {Item. Content="Little Dream I love you"; awaitConn. Updateasync (item); } await NewMessagedialog ("Update Data Success! "). Showasync ();
Windows Phone 8.1 development sqlite-querying data
Sqliteasyncconnection conn = getconn (); var query = conn. Table<note>(); var await query. Tolistasync (); New observablecollection<note>(result); = notes; await New Messagedialog (" query data Success! "). Showasync ();
Windows Phone 8.1 Development SQLite database operation detailed source download:
Click I download!
The Windows Phone 8.1 development SQLite database operation detailed