Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
SQLiteOpenHelper(
Context context,
String name,
SQLiteDatabase.CursorFactory factory, int version)
Create a helper object to create, open, and/or manage a database.
| |||||||||||
SQLiteOpenHelper(
Context context,
String name,
SQLiteDatabase.CursorFactory factory, int version,
DatabaseErrorHandler errorHandler)
Create a helper object to create, open, and/or manage a database.
|
public SQLiteDatabase getReadableDatabase ()
Create and/or open a database. This will be the same object returned by getWritableDatabase()
unless some problem, such as a full disk, requires the database to be opened read-only. In that case, a read-only database object will be returned. If the problem is fixed, a future call to getWritableDatabase()
may succeed, in which case the read-only database object will be closed and the read/write object will be returned in the future.
Like getWritableDatabase()
, this method may take a long time to return, so you should not call it from the application main thread, including from ContentProvider.onCreate()
.
Returns | |
---|---|
SQLiteDatabase | a database object valid until getWritableDatabase() or close() is called. |
Throws | |
---|---|
SQLiteException | if the database cannot be opened |
public SQLiteDatabase getWritableDatabase ()
Create and/or open a database that will be used for reading and writing. The first time this is called, the database will be opened and onCreate(SQLiteDatabase)
, onUpgrade(SQLiteDatabase, int, int)
and/oronOpen(SQLiteDatabase)
will be called.
Once opened successfully, the database is cached, so you can call this method every time you need to write to the database. (Make sure to call close()
when you no longer need the database.) Errors such as bad permissions or a full disk may cause this method to fail, but future attempts may succeed if the problem is fixed.
Database upgrade may take a long time, you should not call this method from the application main thread, including from ContentProvider.onCreate()
.
Returns | |
---|---|
SQLiteDatabase | a read/write database object valid until close() is called |
Throws | |
---|---|
SQLiteException | if the database cannot be opened for writing
Pasted from: https://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#getWritableDatabase()
|
create table Book (
id integer primary key autoincrement,//integer表示整形,real表示浮点型,text表示文本类型,blob表示二进制类型、
//使用primary key 将ID设为主键,并用autoincrement表示id列是自增长的
author text,
price real,
pages integer,
name text)
package com.example.databasetest;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
/**
* Created by 孙伟 on 30/03/2016.
*/
public class MyDatabaseHelper extends SQLiteOpenHelper {
//将建表语句定义成了一个字符串常量
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);
Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public void execSQL (String sql)
Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.
It has no means to return any data (such as the number of affected rows). Instead, you're encouraged to use insert(String, String, ContentValues)
, update(String, ContentValues, String, String[])
, et al, when possible.
When using enableWriteAheadLogging()
, journal_mode is automatically managed by this class. So, do not set journal_mode using "PRAGMA journal_mode'" statement if your app is using enableWriteAheadLogging()
Parameters | |
---|---|
sql | String : the SQL statement to be executed. Multiple statements separated by semicolons are not supported. |
Throws | |
---|---|
SQLException | if the SQL string is invalid |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/create_database"/>
</LinearLayout>
package com.example.databasetest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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);
// 数据库名定为BookStore.db,版本号为1,dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 1);
Button createDatabase = (Button) findViewById(R.id.create_database);
createDatabase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 第一次点击 Create database按钮时会检测没有这个数据库
dbHelper.getWritableDatabase();
}
});
}
}
create table Category {
id integer primary key autoincrement,
category_name text,
category_code integer)
package com.example.databasetest;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
/**
* Created by 孙伟 on 30/03/2016.
*/
public class MyDatabaseHelper extends SQLiteOpenHelper {
public static final String CREATE_BOOK ="create table book ("
+ "id integer primary key autoincrement, "
+ "author text, "
+ "price real, "
+ "pages integer, "
+ "name text)";
public static final String CREATE_CATEGORY = "create table Category ("
+ "id integer primary key autoincrement, "
+ "category_name text, "
+ "category_code integer)";
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);
db.execSQL(CREATE_CATEGORY);
Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists Book");
db.execSQL("drop table if exists Category");
onCreate(db);
}
//新的数据库名定为BookStore.db,版本号为2
dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 2);