Android development-Database Application-SQLite OpenHelper-Retrieve)

Source: Internet
Author: User

/* (Start of program header annotation)
* Copyright and version Declaration of the program
* Copyright (c) 2011, a student from the computer College of Yantai University
* All rights reserved.
* File name: Database Application-Retrieve)
* Author: Lei hengxin
* Completion date: January 1, August 14, 2012
* Version No.: V1.0
* Description of tasks and Solutions
* Input description:
* Problem description:
* Program output:
* End the comment in the program Header
*/
 
 
The previously used "db. rawQuery" statement can be used for SQL queries directly (the program is as follows ),
[Java]
Public Cursor getall (){
Return db. rawQuery ("SELECT * FROM notes", null );

}
Return the query result pointer. If you want to implement the search function, you can use the more flexible "query" method in the program to replace the "rawQuery" method that runs the "SQL" statement directly.
 
The modified "DummyDbAdapter. java" file is as follows:
[Java]
Package com. demo. android. dummynote;
 
Import android. content. Context;
Import android. database. Cursor;
Import android. database. SQLException;
Import android. database. sqlite. SQLiteDatabase;
Import android. database. sqlite. SQLiteOpenHelper;
Import android. database. sqlite. SQLiteDatabase. CursorFactory;
 
Public class NotesDbAdapter {
Private static final String DATABASE_NAME = "notes. db ";
Private static final int DATABASE_VERSION = 1;
Private static final String DATABASE_TABLE = "notes ";
Private static final String DATABASE_CREATE = "creat table notes ("
+ "_ Id integer primary key," + "note TEXT," + "created INTEGER ,"
+ "Modified INTEGER" + ");";
 
Private static class DatabaseHelper extends SQLiteOpenHelper {
Public DatabaseHelper (Context context ){
Super (context, DATABASE_NAME, null, DATABASE_VERSION );
// TODO Auto-generated constructor stub
}
 
@ Override
Public void onCreate (SQLiteDatabase db ){
// TODO Auto-generated method stub
Db.exe cSQL (DATABASE_CREATE );
 
}
 
@ Override
Public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion ){
// TODO Auto-generated method stub
Db.exe cSQL ("drop table if exists" + DATABASE_TABLE );
OnCreate (db );
}
}
 
Private Context mCtx = null;
Private DatabaseHelper dbHelper;
Private SQLiteDatabase db;
 
Public NotesDbAdapter (Context ctx ){
This. mCtx = ctx;
}
 
Public NotesDbAdapter open () throws SQLException {
DbHelper = new DatabaseHelper (mCtx );
Db = dbHelper. getWritableDatabase ();
Return this;
}
 
Public void close (){
DbHelper. close ();
}
 
Private static final String KEY_ROWID = "_ id ";
Static final String KEY_NOTE = "note ";
Private static final String KEY_CREATED = "created ";

String [] strCols = new String [] {
KEY_ROWID,
KEY_NOTE,
KEY_CREATED,
};
 
// Get all entries
Public Cursor getall (){
Return db. query (DATABASE_TABLE, // Which table to Select
StrCols, // Which columns to return
Null, // WHERE clause
Null, // WHERE arguments
Null, // group by clause
Null, // HAVING clause
Null); // Order-by clause
 
}
}

Refactoring:
Because the "" symbol string is used only once in the "" statement, a "" variable is rarely declared to save memory space, the two sections "" and "" will be written together. You can also handle the situation in other programs.
The modified "DummyDbAdapter. java" file is as follows:
[Java]
Package com. demo. android. dummynote;
 
Import android. content. Context;
Import android. database. Cursor;
Import android. database. SQLException;
Import android. database. sqlite. SQLiteDatabase;
Import android. database. sqlite. SQLiteOpenHelper;
Import android. database. sqlite. SQLiteDatabase. CursorFactory;
 
Public class NotesDbAdapter {
Private static final String DATABASE_NAME = "notes. db ";
Private static final int DATABASE_VERSION = 1;
Private static final String DATABASE_TABLE = "notes ";
Private static final String DATABASE_CREATE = "creat table notes ("
+ "_ Id integer primary key," + "note TEXT," + "created INTEGER ,"
+ "Modified INTEGER" + ");";
 
Private static class DatabaseHelper extends SQLiteOpenHelper {
Public DatabaseHelper (Context context ){
Super (context, DATABASE_NAME, null, DATABASE_VERSION );
// TODO Auto-generated constructor stub
}
 
@ Override
Public void onCreate (SQLiteDatabase db ){
// TODO Auto-generated method stub
Db.exe cSQL (DATABASE_CREATE );
 
}
 
@ Override
Public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion ){
// TODO Auto-generated method stub
Db.exe cSQL ("drop table if exists" + DATABASE_TABLE );
OnCreate (db );
}
}
 
Private Context mCtx = null;
Private DatabaseHelper dbHelper;
Private SQLiteDatabase db;
 
Public NotesDbAdapter (Context ctx ){
This. mCtx = ctx;
}
 
Public NotesDbAdapter open () throws SQLException {
DbHelper = new DatabaseHelper (mCtx );
Db = dbHelper. getWritableDatabase ();
Return this;
}
 
Public void close (){
DbHelper. close ();
}
 
Private static final String KEY_ROWID = "_ id ";
Static final String KEY_NOTE = "note ";
Private static final String KEY_CREATED = "created ";

// Get all entries
Public Cursor getall (){
Return db. query (DATABASE_TABLE, // Which table to Select
New String [] {KEY_ROWID, KEY_NOTE, KEY_CREATED}, // Which columns to return
Null, // WHERE clause
Null, // WHERE arguments
Null, // group by clause
Null, // HAVING clause
Null); // Order-by clause
 
}
}


Author: leihengxin

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.