The fifth day of Android development for SQLite database operations

Source: Internet
Author: User

 

Public class dbhelper extends sqliteopenhelper {/*** database name */Private Static final string db_name = "mydb. DB ";/*** database version */Private Static final int db_version = 1; Public dbhelper (context, string name, cursorfactory factory, int version) {super (context, name, factory, version);} public dbhelper (context) {This (context, db_name, null, db_version );} /*** create database */Public void oncreate (sqlitedatabase dB) {string SQL = "CREATE TABLE MERs (ID integer primary key autoincrement, name varchar (20), age INT) "; db.exe csql (SQL);}/*** used to upgrade */Public void onupgrade (sqlitedatabase dB, int oldversion, int newversion) {// todo auto-generated method stub }}

CRUD operation

Public class crud {dbhelper helper; Public CRUD (context CTX) {helper = new dbhelper (CTX) ;}/ *** insert operation */Public void insert (string name, int age) {string SQL = "insert into MERs (name, age) values (?,?) "; Sqlitedatabase DB = helper. getwritabledatabase (); db.exe csql (SQL, new object [] {name, age});}/*** update operation */Public void Update (string name, int age, int ID) {string SQL = "Update MERs set name =?, Age =? Where id =? "; Sqlitedatabase DB = helper. getwritabledatabase (); db.exe csql (SQL, new object [] {name, age, Id});}/*** delete operation */Public void Delete (INT ID) {string SQL = "delete from MERs where id =? "; Sqlitedatabase DB = helper. getwritabledatabase (); db.exe csql (SQL, new object [] {ID});}/*** query all customer information */public list <object []> findall () {list <object []> List = new arraylist <object []> (); object [] O = NULL; string SQL = "select * from MERs "; sqlitedatabase DB = helper. getwritabledatabase (); cursor cur = dB. rawquery (SQL, null); While (cur. movetonext () {o = new object [3]; O [0] = cur. getint (cur. getcolumnindex ("ID"); O [1] = cur. getstring (cur. getcolumnindex ("name"); O [2] = cur. getint (cur. getcolumnindex ("Age"); list. add (o);} cur. close (); return list ;}}

Mobile development QQ group: 59516399

Csdn download link: http://download.csdn.net/detail/wenwei19861106/4970365

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.