[安卓] 数据库的Android仲 – P1陶数据库
大家好, 今天我就开始引导你一个系列 数据库的Android仲. 要具体生动,我会引导你提出申请 “注意” 一个完整的用户数据库.
这个系列的剧本我会用英文写. 然而,这是他的英式写作 “越南语翻译” 所以,你会很容易跟随.
这里: Android的工作室 1.2.2
Android的SDK 5.1.1
闵SDK: 4.0 (安卓 4.0 以上将被用于应用程序)
如果在Java或C#SQL我们熟悉或MySQL存储数据,我们使用SQLite在Android中存储.
在其他系列定期带领的家伙,你使用类 SQLiteDatabase 操纵, 但我会教你使用的组合 SQLiteOpenHelper 要能够进行最全面的升级是在过程中的应用,我们可以很容易地更新数据库.
步 1: 创建对象类存储在数据库
在这一步中,我们创建类往往是数据库表. 例如,所需的类tb_note表注意事项.
package cachhoc.net.tut.demodatabase;
public class Note {
private long id;
private String title;
private String content;
public String getTitle() {
return title;
}
public Note setTitle(String title) {
this.title = title;
return this;
}
public long getId() {
return id;
}
public Note setId(long id) {
this.id = id;
return this;
}
public String getContent() {
return content;
}
public Note setContent(String content) {
this.content = content;
return this;
}
}
在上面的代码中,你要注意设置方法. 他的回报是注满 (类它总是) 这使我们能够方便的一组值,并减少运行时间程序.
步 2: 初始化数据库
在这一步中,我们将实现数据库. 你创建一个类 DatabaseHelper 遗产 SQLiteOpenHelper 如下:
package cachhoc.net.tut.demodatabase;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "note.db";
/**
* table note contain id, title, content
*/
public static final String TABLE_NOTE = "tb_note";
public static final String KEY_ID_NOTE = "id";
public static final String KEY_TITLE_NOTE = "title";
public static final String KEY_CONTENT_NOTE = "content";
/**
* string for create table note
*/
public static final String CREATE_TABLE_NOTE =
"CREATE TABLE " + TABLE_NOTE + "(" +
KEY_ID_NOTE + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL" +
", " + KEY_TITLE_NOTE + " TEXT NOT NULL" +
"," + KEY_CONTENT_NOTE + " TEXT NOT NULL" +
")";
/**
* value for update database
*/
public static final int DATA_VERSION = 1;
/**
* Sqlite database
*/
private SQLiteDatabase db;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATA_VERSION);
}
/**
* create db when app start, and only call when database don't create
* When database created, it will not call
*/
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE_NOTE);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* call when change DATA_VERSION
* help we update database
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
/**
* open database
*/
public void open() {
try {
db = getWritableDatabase();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* close database
*/
public void close() {
if (db != null && db.isOpen()) {
try {
db.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
在这里,您可以注意以下几点:
- 变量 DATABASE_NAME: 是我们数据库的名称. 每个应用程序应该只有一个数据库.
-
表 tb_note 我们的学校 ID, 称号 和 内容 分别为,我们有串 CREATE_TABLE_NOTE 创建表
-
变量 DATA_VERSION 是版本数据库. 当你要更改数据库中的以下版本,我们不得不提高了数据库版本更新.
-
变量 DB 对象类 SQLiteDatabase 将变量来操纵数据库处理指令
-
方法 的onCreate 从类覆盖 SQLiteOpenHelper 将帮助我们创造DATABSE.
-
方法 onUpgrade 将帮助我们更新数据库的时候 DATA_VERSION 变化.
-
该方法 开放, 近 关闭和打开数据库.
步 3: 写入方法基本的数据库查询
在这一步中,我们会写一些基本的方法来获取数据, 插入数据, 更新或删除.
/************************* method work with database *******************/
/**
* get all row of table with sql command then return cursor
* cursor move to frist to redy for get data
*/
public Cursor getAll(String sql) {
open();
Cursor cursor = db.rawQuery(sql, null);
if (cursor != null) {
cursor.moveToFirst();
}
close();
return cursor;
}
/**
* insert contentvaluse to table
*
* @param values value of data want insert
* @return index row insert
*/
public long insert(String table, ContentValues values) {
open();
long index = db.insert(table, null, values);
close();
return index;
}
/**
* update values to table
*
* @return index row update
*/
public boolean update(String table, ContentValues values, String where) {
open();
long index = db.update(table, values, where, null);
close();
return index > 0;
}
/**
* delete id row of table
*/
public boolean delete(String table, String where) {
open();
long index = db.delete(table, where, null);
close();
return index > 0;
}
/************************* end of method work with database *******************/
您可能会注意到一些新的特点相比,SQL和MySQL
- 在我们的方法返回GETALL光标, 它像一个指针指向该记录 (排) 原始表,指着它没有,所以我们需要将其设置为指向第一行用命令 cursor.moveToFirst();
-
在插入模式我们称之为 db.insert(表, 空值, 值), 其中的值是类的一个对象 ContentValues 内容插入到表. 我们将学习如何使用它在步骤 3.
-
插入, 更新和删除它们返回一个整数类型,只要记录插入位置, 更新或删除. 如果没有公共记录是由 (错误) 它会返回是 0.
步 4: 写查询方法为表
一旦这样做了 2 因为我们已经完成了创建和访问数据库,可以跳过这一步,如果你想, 但我们会遇到另一个类的一些问题或冗长的治疗,当我们调用方法来查询. 因此,为了使一个简单的方法,我们应该为每个特定的表的查询方法. 这里是检索表tb_note的方法.
/************************* method work with note table *******************/
/**
* get Note by sql command
*
* @param sql sql to get note
*/
public Note getNote(String sql) {
Note note = null;
Cursor cursor = getAll(sql);
if (cursor != null) {
note = cursorToNote(cursor);
cursor.close();
}
return note;
}
/**
* @param sql get all notes with sql command
* @return arraylist of note
*/
public ArrayList<Note> getListNote(String sql) {
ArrayList<Note> list = new ArrayList<>();
Cursor cursor = getAll(sql);
while (!cursor.isAfterLast()) {
list.add(cursorToNote(cursor));
cursor.moveToNext();
}
cursor.close();
return list;
}
/**
* insert note to table
*
* @param note note to insert
* @return id of note
*/
public long insertNote(Note note) {
return insert(TABLE_NOTE, noteToValues(note));
}
/**
* @param note note to update
* @return id of note update
*/
public boolean updateNote(Note note) {
return update(TABLE_NOTE, noteToValues(note), KEY_ID_NOTE + " = " + note.getId());
}
/**
* delete id row of table
*/
public boolean deleteNote(String where) {
return delete(TABLE_NOTE, where);
}
/**
* convert note to contentvalues
* don't put id of note because
* when insert id will auto create
* when update we don't update id
*/
private ContentValues noteToValues(Note note) {
ContentValues values = new ContentValues();
values.put(KEY_TITLE_NOTE, note.getTitle());
values.put(KEY_CONTENT_NOTE, note.getContent());
return values;
}
/**
* convert cursor to note
*/
private Note cursorToNote(Cursor cursor) {
Note note = new Note();
note.setId(cursor.getInt(cursor.getColumnIndex(KEY_ID_NOTE)))
.setTitle(cursor.getString(cursor.getColumnIndex(KEY_TITLE_NOTE)))
.setContent(cursor.getString(cursor.getColumnIndex(KEY_CONTENT_NOTE)));
return note;
}
/************************* end of method work note table *******************/
在上面的代码,你发表过评论很清楚, 你试着去了解.
全码
package cachhoc.net.tut.demodatabase;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "note.db";
/**
* table note contain id, title, content
*/
public static final String TABLE_NOTE = "tb_note";
public static final String KEY_ID_NOTE = "id";
public static final String KEY_TITLE_NOTE = "title";
public static final String KEY_CONTENT_NOTE = "content";
/**
* string for create table note
*/
public static final String CREATE_TABLE_NOTE =
"CREATE TABLE " + TABLE_NOTE + "(" +
KEY_ID_NOTE + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL" +
", " + KEY_TITLE_NOTE + " TEXT NOT NULL" +
"," + KEY_CONTENT_NOTE + " TEXT NOT NULL" +
")";
/**
* value for update database
*/
public static final int DATA_VERSION = 1;
/**
* Sqlite database
*/
private SQLiteDatabase db;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATA_VERSION);
}
/**
* create db when app start, and only call when database don't create
* When database created, it will not call
*/
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE_NOTE);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* call when change DATA_VERSION
* help we update database
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
/**
* open database
*/
public void open() {
try {
db = getWritableDatabase();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* close database
*/
public void close() {
if (db != null && db.isOpen()) {
try {
db.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/************************* method work with database *******************/
/**
* get all row of table with sql command then return cursor
* cursor move to frist to redy for get data
*/
public Cursor getAll(String sql) {
open();
Cursor cursor = db.rawQuery(sql, null);
if (cursor != null) {
cursor.moveToFirst();
}
close();
return cursor;
}
/**
* insert contentvaluse to table
*
* @param values value of data want insert
* @return index row insert
*/
public long insert(String table, ContentValues values) {
open();
long index = db.insert(table, null, values);
close();
return index;
}
/**
* update values to table
*
* @return index row update
*/
public boolean update(String table, ContentValues values, String where) {
open();
long index = db.update(table, values, where, null);
close();
return index > 0;
}
/**
* delete id row of table
*/
public boolean delete(String table, String where) {
open();
long index = db.delete(table, where, null);
close();
return index > 0;
}
/************************* end of method work with database *******************/
/************************* method work with note table *******************/
/**
* get Note by sql command
*
* @param sql sql to get note
*/
public Note getNote(String sql) {
Note note = null;
Cursor cursor = getAll(sql);
if (cursor != null) {
note = cursorToNote(cursor);
cursor.close();
}
return note;
}
/**
* @param sql get all notes with sql command
* @return arraylist of note
*/
public ArrayList<Note> getListNote(String sql) {
ArrayList<Note> list = new ArrayList<>();
Cursor cursor = getAll(sql);
while (!cursor.isAfterLast()) {
list.add(cursorToNote(cursor));
cursor.moveToNext();
}
cursor.close();
return list;
}
/**
* insert note to table
*
* @param note note to insert
* @return id of note
*/
public long insertNote(Note note) {
return insert(TABLE_NOTE, noteToValues(note));
}
/**
* @param note note to update
* @return id of note update
*/
public boolean updateNote(Note note) {
return update(TABLE_NOTE, noteToValues(note), KEY_ID_NOTE + " = " + note.getId());
}
/**
* delete id row of table
*/
public boolean deleteNote(String where) {
return delete(TABLE_NOTE, where);
}
/**
* convert note to contentvalues
* don't put id of note because
* when insert id will auto create
* when update we don't update id
*/
private ContentValues noteToValues(Note note) {
ContentValues values = new ContentValues();
values.put(KEY_TITLE_NOTE, note.getTitle());
values.put(KEY_CONTENT_NOTE, note.getContent());
return values;
}
/**
* convert cursor to note
*/
private Note cursorToNote(Cursor cursor) {
Note note = new Note();
note.setId(cursor.getInt(cursor.getColumnIndex(KEY_ID_NOTE)))
.setTitle(cursor.getString(cursor.getColumnIndex(KEY_TITLE_NOTE)))
.setContent(cursor.getString(cursor.getColumnIndex(KEY_CONTENT_NOTE)));
return note;
}
/************************* end of method work note table *******************/
}
部分 2 我会引导你,使界面风格材质活动设计并开始创建一个完整的应用程序.
在本教程的帖子 数据库的Android仲 由 nguyenvanquan7826
教程安卓
[RPS-包括交=”4210″ 简码=”假”]



希望基本组成部分,Android将有超过帖子.
我想问的是,如果管理层想添加一个不同的注意只是不希望保存的电话号码本笔记, 我们必须添加其他DatabaseHelper或创建新的公共类,但共享注意这个数据库 ?
您可以创建 1 其他表也DJK. 但是,我们很少创建纳克在多个数据库 1 应用程序
我得到了它. 谢谢.
我想使用数据库中的数据作为 autocompleteTexview 的项目,如 listview
有太多.
兄弟为孩子问自己是没有办法获得信息出表为列和列的格式是不是 ?