SQLite是安卓内置的一款轻量级数据库,不仅支持标准的SQL语法,还遵循了数据库的ACID事务,它比一般的数据库更简单,不需要设置账号密码就能使用。SQLiteOpenHelper类可以非常简单的对数据库进行创建和升级。SQLiteOpenHelper类是一个抽象类,如果想要使用它,就要创建一个自己的帮助类去继承它。SQLiteOpenHelper有两个抽象方法,分别是OnCreate()和OnUpgrade()。还有两个非常重要的实例方法,getReadableDatabase()和getWriteableDatabase()。当数据库不可写入的时候,getReadableDatabase()方法返回的对象将以只读的方式去打开数据库,而getWriteableDatabase()将出现异常。
SQLite有四种数据类型,integer表示整形,real表示浮点型,text表示文本类型,blob表示二进制类型。
数据库的基本元素:
1,关系数据库
2,表:同一类记录的集合
3,字段:对应实体的属性
4,元祖:也叫做记录,表中的每一行
package com.example.lenovo.my_sqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDatabaseHelper extends SQLiteOpenHelper {
public final String DB_NAME="Book";
public static final String CREATE_BOOK="create table Book("
+"id integer primary key autoincrement,"
+"author text,"
+"price real,"
+"pages integer,"
+"name text)";
private Context mContext;
public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
package com.example.lenovo.my_sqlite;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private MyDatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper=new MyDatabaseHelper(this,MyDatabaseHelper.CREATE_BOOK,null,1);
Button createDatabase=findViewById(R.id.button);
Button addData=findViewById(R.id.add_data);
addData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name","the Da Vinci Code");
values.put("author","Dan Brown");
values.put("pages",454);
values.put("price",16.96);
db.insert("Book",null,values);
values.clear();
values.put("name","the Lost Symbol");
values.put("author","Dan Brown");
values.put("pages",488);
values.put("price",19.95);
db.insert("Book",null,values);
}
});
createDatabase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper.getWritableDatabase();
}
});
Button queryButton=findViewById(R.id.query_data);
queryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
Cursor cursor=db.query("Book",null,null,null,
null,null,null);
if(cursor.moveToFirst()){
do{
String name=cursor.getString(cursor.getColumnIndex("name"));
String author=cursor.getString(cursor.getColumnIndex("author"));
int pages=cursor.getInt(cursor.getColumnIndex("pages"));
double price=cursor.getDouble(cursor.getColumnIndex("price"));
Log.d("MainActivity","book name is"+name);
}while (cursor.moveToNext());
}
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="create database" />
<Button
android:id="@+id/add_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add data" />
<Button
android:id="@+id/query_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="query data" />
</LinearLayout>