Android Operation SQLite Database

Source: Internet
Author: User

1.

Package com.exp.contentprividerstest.db;

Import Android.content.Context;
Import Android.database.sqlite.SQLiteDatabase;
Import Android.database.sqlite.SQLiteOpenHelper;

public class Dburil extends Sqliteopenhelper {

    public dburil (context context) {
& nbsp;       super (context, "user.db", NULL, 2);
        System.out.println ("Asdfasdfds");
    }

     @Override
    public void OnCreate (Sqlitedatabase db) {
        string sql= "CREATE TABLE User (ID int,name varchar (32))";
        system.out.println (SQL);
        db.execsql (SQL);
    }

     @Override
    public void Onupgrade (sqlitedatabase arg0, int arg1, int ARG2) {

    }

}

2.

Package com.exp.contentprividerstest.test;

Import Com.exp.contentprividerstest.db.DBUril;

Import android.content.ContentValues;
Import Android.database.Cursor;
Import Android.database.sqlite.SQLiteDatabase;
Import Android.test.AndroidTestCase;

public class DBTest extends androidtestcase{

public void Testinsert () {
Dburil dburil=new Dburil (GetContext ());
Sqlitedatabase database=dburil.getreadabledatabase ();

for (int i=0;i<10;i++) {
Contentvalues values=new contentvalues ();
Values.put ("id", i);
Values.put ("name", "name1:" +i);
Database.insert ("user", null, values);
}
}

public void Insert1 () {
Dburil dburil=new Dburil (GetContext ());
Sqlitedatabase database=dburil.getreadabledatabase ();

for (int i=0;i<10;i++) {
String []bindargs={"+i", "name1000" +i};
Database.execsql ("INSERT into user values (?,?)", Bindargs);
}
}

public void Delete () {
Dburil dburil=new Dburil (GetContext ());
Sqlitedatabase database=dburil.getreadabledatabase ();
Database.delete ("User", "id=?", New string[]{"4"});
}

public void Delete1 () {
Dburil dburil=new Dburil (GetContext ());
Sqlitedatabase database=dburil.getreadabledatabase ();
Database.delete ("User", "id=?", New string[]{"4"});
String sql= "Delete from user where id=?";
String bindargs[]={"10003"};
Database.execsql (SQL, Bindargs);
}

public void Update () {
Dburil dburil=new Dburil (GetContext ());
Sqlitedatabase database=dburil.getreadabledatabase ();
Contentvalues values=new contentvalues ();
Values.put ("name", "name3:333333333333333333");
Database.update ("User", Values, "id=", New string[]{"3"});
}

public void Update1 () {
Dburil dburil=new Dburil (GetContext ());
Sqlitedatabase database=dburil.getreadabledatabase ();
String sql= "Update user set name=?" where id=? ";
String bindargs[]={"1000000000000", "1003"};
Database.execsql (SQL, Bindargs);
}

public void query () {
Dburil dburil=new Dburil (GetContext ());
Sqlitedatabase database=dburil.getreadabledatabase ();
Cursor cursor=database.query ("user", NULL, NULL, NULL, NULL, NULL, NULL);
while (Cursor.movetonext ()) {
int Id=cursor.getint (CURSOR.GETCOLUMNINDEX ("id"));
String name=cursor.getstring (Cursor.getcolumnindex ("name"));
System.out.println (id+ "=" +name);
}
}

public void Query1 () {
Dburil dburil=new Dburil (GetContext ());
Sqlitedatabase database=dburil.getreadabledatabase ();
String sql= "SELECT * from user where id=?";
Cursor cursor=database.rawquery (SQL, New string[]{"10009"});
while (Cursor.movetonext ()) {
int Id=cursor.getint (CURSOR.GETCOLUMNINDEX ("id"));
String name=cursor.getstring (Cursor.getcolumnindex ("name"));
System.out.println (id+ "=" +name);
}
}
}



Android Operation SQLite Database

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.