A simple example of connecting Android to the SQLite Database

Source: Internet
Author: User

Background:

SQLite is a very popular embedded database. It provides a refreshing SQL interface, a relatively small memory usage and high-speed response. Even more Happy, it is still free of charge, you can use it as much as you like. Many companies (such as Adobe, Apple, Google, Sun, and Symbian) and open-source projects (Mozilla, PHP, and Python) all assemble SQLite in the product.

In Android, SQLite is integrated into Android runtime. Every Android application can happily use the SQLite database. If you are familiar with JDBC, this process is more comfortable.

SQLite3 features

Compared with traditional relational databases:

SQL statement: SELECT (query), INSERT (add data), UPDATE (modify data), CREATE (CREATE TABLE), DROP (delete table)

Data Type: Case Insensitive, TEXT, NUMERIC, INTEGER, REAL, NONE)

None: foreign key (foreign key constraint), right outer join, full outer join, alter table (modifying columns in the TABLE)

Program:

1. Start the eclipse and android virtual machines and use the adb shell command line to create directories, SQLite databases, and tables.

1. Compile runadb. bat

Path D: \ Program Design \ android \ eclipse 3.7 \ android-sdks \ platform-tools

Adb shell (you can also enter it directly in the DOS environment );

2. Create a folder

Create cqvie.edu.cn (name in the project) in the data/data directory ),

Mkdir cqvie.edu.cn

Cd cqvie.edu.cn create a folder databases under this directory

Mkdir databases

Cd databases

3. Create a database

Sqlite3 test. db

Create table name ("no" integer, "name" text); // create a table

Select * from sqlite_master where type = "table" and name = "table name"; // query the table structure

Insert into table name values (1, "Zhang San ");

. Explain ON

Select * from table name; // query

2. Write an android program to add and query table records

1. Create a DBHelper class for database operations

Right-click the cqvie.edu.cn package and create a class.

 

The class name is DBHelper, inherited from "android. database. sqlite. SQLiteOpenHelper". You can click the button to find the base class name.

2. User Interface

3. write code to add a query

Public VoidOnCreate (Bundle savedInstanceState ){

Super. OnCreate (savedInstanceState );

SetContentView (R. layout.Main);

Button btnAdd = (Button) findViewById (R. id.BtnAdd);

Button btnQuery = (Button) findViewById (R. id.BtnQuery);

BtnAdd. setOnClickListener (This);

BtnQuery. setOnClickListener (This);

}

 

Public VoidOnClick (View arg0 ){

//TODOAuto-generated method stub

DBHelper helper =NewDBHelper (This, "Test. db ",Null, 1 );

SQLiteDatabase db = helper. getWritableDatabase ();

Button btn = (Button) arg0;

IntId = btn. getId ();

If(Id = R. id.BtnAdd){

String SQL = "insert into count values (2222 )";

Db.exe cSQL (SQL );

}

Else If(Id = R. id.BtnQuery){

Cursor cursor = db. query ("count ",NewString [] {"*"}, "name =? ",NewString [] {"2222 "},Null,Null,Null);

String s = "Query Result \ n ";

While(Cursor. moveToNext ()){

Int= Cursor. getInt (cursor. getColumnIndex ("no "));

String name = cursor. getString (cursor. getColumnIndex ("name "));

S + = no + "," + name + "\ n ";

}

EditText Text = (EditText) findViewById (R. id.Text);

Text. setText (s );

}

}

}

Before performing a database write test, you must open the write permission for the database file test. db. Otherwise, an error will occur.

Write chmod 777 test. db in the data/cqvie.edu.cn/databasesdirectory.

When the identifier is "#", you can directly write this chmod 777 test. db

When the identifier is "sqlite>", exit. exit first, and then write this statement chmod 777 test. db.

4. Result Display

 

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.