Zhao Yazhi _ using Sqlitedatabase to manipulate SQLite database and transaction

Source: Internet
Author: User
Tags sqlite database

Knowledge Point Detailed: http://blog.csdn.net/zhaoyazhi2129/article/details/9025995

Specific code:

Mainactivity.java
Package Com.example.android_sqlite;import Android.app.activity;import Android.os.bundle;import com.example.android _sqlite.dao.impl.userdaoimpls;import Com.example.android_sqlite.database.databasehelper;public class MainActivity Extends Activity {private userdaoimpls userdaoimpls; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);//CREATE DATABASE Databasehelper DH = new Databasehelper (this); userdaoimpls = new Userdaoimpls (DH);}}


Users.java
Package Com.example.android_sqlite.domain;import Java.io.serializable;public class Users implements serializable{ Private static final Long Serialversionuid = 1l;private integer userid;private String username;private integer USERAGE;PRI Vate Double usersalary;public Users () {super ();} Public Users (integer userId, String userName, integer userage,double usersalary) {super (); this.userid = UserId; This.username = Username;this.userage = Userage;this.usersalary = usersalary;} Public Integer GetUserId () {return userId;} public void Setuserid (Integer userId) {this.userid = userId;} Public String GetUserName () {return userName;} public void Setusername (String userName) {this.username = UserName;} Public Integer Getuserage () {return userage;} public void Setuserage (Integer userage) {this.userage = Userage;} Public Double getusersalary () {return usersalary;} public void Setusersalary (Double usersalary) {this.usersalary = usersalary;} @Overridepublic String toString () {return "Users [userid=" + UserId + ", uSername= "+ username+", userage= "+ Userage +", usersalary= "+ Usersalary +"] ";}} 


Basedao.java
Package Com.example.android_sqlite.dao;import Java.util.list;public Interface basedao<t, pk> {/** * Insert entity Object *  * @param entity * @return */public Boolean insert (T entity);/** * Update Entity Object *  * @param entity * @return */public Boolean Update (t entity);/** * Delete Entity Object *  * @param entity * @return */public Boolean delete (t entity);/** * Delete object by ID *  * @pa Ram ID * @return */public boolean Deletebyid (PK ID);/** * Find object by ID *  * @param ID * @return */public T FindByID (PK ID); /** * Find all *  * @return */public list<t> findAll ();}


Userdao.java
Package Com.example.android_sqlite.dao;import Com.example.android_sqlite.domain.users;public Interface UserDao Extends Basedao<users, integer>{}


Userdaoimple.java
Package Com.example.android_sqlite.dao.impl;import Java.util.arraylist;import Java.util.list;import Android.database.cursor;import Android.database.sqlite.sqlitedatabase;import com.example.android_ Sqlite.dao.userdao;import Com.example.android_sqlite.database.databasehelper;import com.example.android_ Sqlite.domain.users;public class Userdaoimpls implements Userdao {//Dependent object private Databasehelper dh;// The public Userdaoimpls (Databasehelper databasehelper) {THIS.DH = Databasehelper) is instantiated through the constructor of the database; @Overridepublic Boolean insert (Users entity) {//Gets the database operand try {sqlitedatabase db = Dh.getwritabledatabase ();/* * Method one * db.  Execsql ("INSERT INTO Users" (username,userage,usersalary) VALUES (' "* + entity.getusername () +" ', "+ entity.getuserage () + "," + * entity.getusersalary () + ")"); *///method Two Db.execsql ("INSERT into users (username,userage,usersalary) VALUES (?,?,?)", new object[] {entity.getusername () , Entity.getuserage (), Entity.getusersalary ()});d b.close ();} catch (Exception e) {e.printstacktrace (); return false;} return true;} @Overridepublic the Boolean update (Users entity) {try {sqlitedatabase db = Dh.getwritabledatabase ();d b.execsql ("Update Users set username=?,userage=?,usersalary=? Where userid=? ", new object[] {entity.getusername (), Entity.getuserage (), Entity.getusersalary (), Entity.getuserid ()} );d b.close ();} catch (Exception e) {e.printstacktrace (); return false;} return true;} @Overridepublic Boolean Delete (Users entity) {return Deletebyid (Entity.getuserid ());} @Overridepublic Boolean Deletebyid (Integer id) {try {sqlitedatabase db = Dh.getwritabledatabase ();d b.execsql ("delete From users where userid=? ", new object[] {ID});d b.close ();} catch (Exception e) {e.printstacktrace (); return false;} return true;} @Overridepublic users FindByID (Integer ID) {users entity = NULL; Sqlitedatabase db = Dh.getwritabledatabase (); Cursor C = db.rawquery ("Select Userid,username,userage,usersalary from Users where userid=?", new string[] {id + ""}); if (C.movetonext ()) {entity = new Users (); Entity.setuserid (C.getint (C.getcolumnindex ("userid"))), Entity.setusername (c.getstring ("userid")); Entity.setuserage (C.getint (C.getcolumnindex ("Userage")), Entity.setusersalary (C.getdouble (C.getColumnIndex (" Usersalary ")));} return entity;} @Overridepublic list<users> FindAll () {list<users> entities = new arraylist<users> (); Sqlitedatabase db = Dh.getwritabledatabase (); Cursor C = db.rawquery ("Select Userid,username,userage,usersalary from users", null) and while (C.movetonext ()) {Users entity = new Users (); Entity.setuserid (C.getint (C.getcolumnindex ("userid")); Entity.setusername (C.getstring ( C.getcolumnindex ("userid")); Entity.setuserage (C.getint (C.getcolumnindex ("Userage")); Entity.setusersalary ( C.getdouble (C.getcolumnindex ("Usersalary"))); Entities.add (entity);} return entities;}}


Databasetese.java
Package Com.example.android_sqlite.test;import Java.util.list;import Com.example.android_sqlite.dao.UserDao; Import Com.example.android_sqlite.dao.impl.userdaoimpls;import Com.example.android_ Sqlite.database.databasehelper;import Com.example.android_sqlite.domain.users;import Android.test.androidtestcase;public class Databaseteses extends Androidtestcase {public void CreateDatabase () { Databasehelper dh = new Databasehelper (GetContext ());d h.getwritabledatabase (); public void Insert () {Databasehelper databasehelper = new Databasehelper (GetContext ()); Userdao Userdao = new Userdaoimpls (databasehelper); Users entity = new users (null, "Iii2", 4000.0); Boolean flag = Userdao.insert (entity); Assertequals (true, flag);} public void Update () {Databasehelper databasehelper = new Databasehelper (GetContext ()); Userdao Userdao = new Userdaoimpls (databasehelper); Users entity = new users (7, "AAA", + 3000.0); Boolean flag = Userdao.update (entity); Assertequals (true, flag);} public void Delete () {DatabaseheLper databasehelper = new Databasehelper (GetContext ()); Userdao Userdao = new Userdaoimpls (databasehelper); Users entity = new users (1, "", NULL, NULL); Boolean flag = Userdao.delete (entity); Assertequals (true, flag);} public void FindByID () {Databasehelper databasehelper = new Databasehelper (GetContext ()); Userdao Userdao = new Userdaoimpls (databasehelper); Users entity = Userdao.findbyid (3); if (entity! = null) {System.out.println (entity.tostring ());} else { SYSTEM.OUT.PRINTLN ("no Message");}} public void FindAll () {Databasehelper databasehelper = new Databasehelper (GetContext ()); Userdao Userdao = new Userdaoimpls (databasehelper); list<users> entities = Userdao.findall (); for (Users entity:entities) {System.out.println (entity.tostring ());}}}



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.