Android SQLite DB Package

Source: Internet
Author: User
Tags sqlite db

Dbopenhelper:

Package com.example.db_demo01. DB;

import Android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import Android.database.sqlite.SQLiteOpenHelper;

Public class Dbopenhelper extends sqliteopenhelper {

Private Static    String Name = "Mydb.db"; Database name

Private Static int            version = 1; Version number

Public Dbopenhelper (Context context) {

Super (Context, name, null, version);

}

@Override

Public void onCreate (sqlitedatabase db) {

String sql = "CREATE table person (ID integer primary key autoincrement, name varchar (+), address varchar (64))";

Db.execsql (SQL); Create a table

}

@Override

Public void Onupgrade (sqlitedatabase arg0, int arg1, int arg2) {

Update the version number at the same time when you update the table structure

}

}

Closure of the DB operation:

Package com.example.db_demo01. DB;

import java.util.ArrayList;

import Java.util.HashMap;

import java.util.List;

import Java.util.Map;

import Android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

Public class DB {

Private Dbopenhelper helper = null;

Public DB (Context context) {

Helper = New dbopenhelper (context);

}

Public Boolean Execsql (String sql) {

boolean flag = false;

Sqlitedatabase database = null;

Try {

Database = Helper.getwritabledatabase ();

Database.execsql (SQL);

Flag = true;

} catch (Exception e) {

E.printstacktrace ();

}finally{

if (Database! = null) {

Database.close ();

}

}

return flag;

}

Insert

Public Boolean Insert (String sql) {

return Execsql (SQL);

}

Delete

Public Boolean Delete (String sql) {

return Execsql (SQL);

}

Update

Public Boolean Update (String sql) {

return Execsql (SQL);

}

Inquire

Public List<map<string, string>> Query (String sql) {

Sqlitedatabase database = null;

list<map<string, string>> list = new arraylist<map<string, string>> ();

Try {

Database = Helper.getwritabledatabase ();

cursor cursor = database.rawquery (SQL, null);

int colums = Cursor.getcolumncount ();

while (Cursor.movetonext ()) {

map<string, string> map = new hashmap<string, string> ();

for (int i=0; i<colums; i++) {

String cols_name = Cursor.getcolumnname (i);

String Cols_value = cursor.getstring (Cursor.getcolumnindex (cols_name));

if (Cols_value = = null) {

Cols_value = "";

}

Map.put (Cols_name, Cols_value);

}

List.add (map);

}

} catch (Exception e) {

E.printstacktrace ();

}finally{

if (Database! = null) {

Database.close ();

}

}

return list;

}

}

Unit Test class:

Package com.example.db_demo01.test;

import java.util.ArrayList;

import java.util.List;

import Java.util.Map;

import android.test.AndroidTestCase;

Import com.example.db_demo01. Db. DB;

Import com.example.db_demo01. Db. Dbopenhelper;

Public class MyTest extends androidtestcase {

Public MyTest () {}

Public void Createdb () {

Dbopenhelper helper = new dbopenhelper (GetContext ());

Helper.getwritabledatabase ();

}

Public void Insert () {

DB db = new db (GetContext ());

String sql = "INSERT into person (name, address) VALUES (' Deng ', ' zhbit ')";

Db. Insert (SQL);

}

Public void Delete () {

DB db = new db (GetContext ());

String sql = "Delete from the person where name = ' Deng '";

System. out. println (db. Delete (SQL));

}

Public void Update () {

DB db = new db (GetContext ());

String sql = "Update person set name = ' Deng 2 ' where name = ' Deng '";

System. out. println (db. Delete (SQL));

}

Public void query () {

DB db = new db (getcontext());

String sql = "SELECT * from person";

list<map<string, string>> list = new arraylist<map<string, string>> ();

List = db. Query (SQL);

System. out. println (List.tostring ());

}

}

Android SQLite DB Package

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.