package com.example.fourteen;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
/** * @author HD * @date 2015-12-7 * @package_name com.example.fourteen * @file_name MainActivity.java */
public class MainActivity extends Activity {
private Button btnCreateTable;
private Button btnUpdatePrice;
private Button btnDeleteData;
private Button btnQueryData;
private Button btnBegintransaction;
private DatabaseHelper databaseHelper;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseHelper = new DatabaseHelper(MainActivity.this, "BookStore.db",
null, 4);
db = databaseHelper.getWritableDatabase();
btnCreateTable = (Button) findViewById(R.id.btnCreateTable);
btnUpdatePrice = (Button) findViewById(R.id.btnUpdatePrice);
btnDeleteData = (Button) findViewById(R.id.btnDeleteData);
btnQueryData = (Button) findViewById(R.id.btnQueryData);
btnBegintransaction = (Button) findViewById(R.id.btnBeginTransaction);
btnCreateTable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentValues values = new ContentValues();
values.put("name", "The First Rows Code");
values.put("author", "郭霖");
values.put("price", 98.50);
values.put("pages", 650);
db.insert("book", null, values);
values.clear();
values.put("name", "Thinking In Java");
values.put("author", "nike");
values.put("pages", 950);
values.put("price", 85.50);
db.insert("book", null, values);
}
});
btnUpdatePrice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentValues values = new ContentValues();
values.put("price", 20.50);
db.update("book", values, "name = ?",
new String[] { "The First Rows Code" });
Toast.makeText(MainActivity.this, "更新数据成功", Toast.LENGTH_SHORT)
.show();
}
});
btnDeleteData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db.delete("book", "pages > ?", new String[]{"600"});
Toast.makeText(MainActivity.this, "删除数据成功", Toast.LENGTH_SHORT).show();
}
});
btnQueryData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor cursor = db.query("book", null, null, null, null, null, null);
if(cursor.moveToNext()){
do {
String name = cursor.getString(cursor.getColumnIndex("name"));
Double price = cursor.getDouble(cursor.getColumnIndex("price"));
int pages = cursor.getInt(cursor.getColumnIndex("pages"));
String author = cursor.getString(cursor.getColumnIndex("author"));
Log.i("hhhd", "书名:"+name+"/n"
+"价格:"+price+"/n"
+"页数:"+pages+"/n"
+"作者:"+author);
} while (cursor.moveToNext());
}
}
});
btnBegintransaction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db.beginTransaction();
try {
db.delete("book", null, null);
ContentValues values = new ContentValues();
values.put("name", "Gitar Test");
values.put("price", 25);
values.put("pages", 450);
values.put("author", "韩栋");
db.insert("book", null, values);
db.setTransactionSuccessful();
} catch (Exception e) {
e.printStackTrace();
}finally{
db.endTransaction();
}
}
});
}
}
package com.example.fourteen;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
/** * @author HD * @date 2015-12-7 * @package_name com.example.fourteen * @file_name DatabaseHelper.java */
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String CREATE_BOOK = "create table book("
+ "id integer primary key autoincrement,"
+ "author text,"
+ "price real,"
+ "pages integer,"
+ "name text,"
+ "category_id integer)";
public static final String CREATE_CATEGORY = "create table Category("
+"id integer primary key autoincrement,"
+"category_name text,"
+"category_code integer)";
public static final String CREATE_MANAGER = "create table Manager("
+"id integer primary key autoincrement,"
+"name text,"
+"age integer,"
+"address text,"
+"salary real)";
private Context context;
public DatabaseHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK);
db.execSQL(CREATE_CATEGORY);
db.execSQL(CREATE_MANAGER);
Toast.makeText(context, "创建表成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch (oldVersion) {
case 1:
db.execSQL(CREATE_CATEGORY);
case 2:
db.execSQL(CREATE_MANAGER);
case 3:
db.execSQL("alter table book add column category_id integer");
default:
}
}
}
package com.example.fourteen;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
/** * @author HD * @date 2015-12-7 * @package_name com.example.fourteen * @file_name DatabaseProvider.java */
public class DatabaseProvider extends ContentProvider {
public static final int BOOK_DIR = 0;
public static final int BOOK_ITEM = 1;
public static final int CATEGORY_DIR = 2;
public static final int CATEGORY_ITEM = 3;
public static UriMatcher uriMatcher;
public static final String AUTHORITY = "com.example.fourteen.provider";
private DatabaseHelper databaseHelper;
static {
uriMatcher.addURI(AUTHORITY, "book", BOOK_DIR);
uriMatcher.addURI(AUTHORITY, "book/#", BOOK_ITEM);
uriMatcher.addURI(AUTHORITY, "Category", CATEGORY_DIR);
uriMatcher.addURI(AUTHORITY, "Category/#", CATEGORY_ITEM);
}
@Override
public boolean onCreate() {
databaseHelper = new DatabaseHelper(getContext(), "BookStore.db", null,
4);
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
Cursor cursor = null;
switch (uriMatcher.match(uri)) {
case BOOK_DIR:
cursor = db.query("book", projection, selection, selectionArgs,
null, null, sortOrder);
break;
case BOOK_ITEM:
String bookId = uri.getPathSegments().get(1);
cursor = db.query("book", projection, "id = ?",
new String[] { bookId }, null, null, sortOrder);
break;
case CATEGORY_DIR:
cursor = db.query("Category", projection, selection, selectionArgs,
null, null, sortOrder);
break;
case CATEGORY_ITEM:
String CategoryId = uri.getPathSegments().get(1);
cursor = db.query("Category", projection, "id = ?",
new String[] { CategoryId }, null, null, sortOrder);
break;
}
return cursor;
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)) {
case BOOK_DIR:
return "vnd.android.cursor.dir/vnd.com.example.fourteen.provider.book";
case BOOK_ITEM:
return "vnd.android.cursor.item/vnd.com.example.fourteen.provider.book";
case CATEGORY_DIR:
return "vnd.android.cursor.dir/vnd.com.example.fourteen.provider.Category";
case CATEGORY_ITEM:
return "vnd.android.cursor.item/vnd.com.example.fourteen.provider.Category";
}
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = databaseHelper.getWritableDatabase();
Uri uriReturn = null;
switch (uriMatcher.match(uri)) {
case BOOK_DIR:
case BOOK_ITEM:
long bookItem = db.insert("book", null, values);
uriReturn = uri.parse("content://" + AUTHORITY + "/book/"
+ bookItem);
break;
case CATEGORY_DIR:
case CATEGORY_ITEM:
long categoryItem = db.insert("Category", null, values);
uriReturn = uri.parse("content://" + AUTHORITY + "/Category"
+ categoryItem);
}
return uriReturn;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = databaseHelper.getWritableDatabase();
int deleteRows = 0;
switch (uriMatcher.match(uri)) {
case BOOK_DIR:
deleteRows = db.delete("book", selection, selectionArgs);
break;
case BOOK_ITEM:
String bookId = uri.getPathSegments().get(1);
deleteRows = db.delete("book", "id = ?", new String[] { bookId });
break;
case CATEGORY_DIR:
deleteRows = db.delete("Category", selection, selectionArgs);
break;
case CATEGORY_ITEM:
String CategoryId = uri.getPathSegments().get(1);
deleteRows = db.delete("Category", "id = ?",
new String[] { CategoryId });
break;
}
return deleteRows;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
SQLiteDatabase db = databaseHelper.getWritableDatabase();
int updataRows = 0;
switch (uriMatcher.match(uri)) {
case BOOK_DIR:
updataRows = db.update("book", values, selection, selectionArgs);
break;
case BOOK_ITEM:
String bookId = uri.getPathSegments().get(1);
updataRows = db.update("book", values, "id = ?", new String[]{bookId});
break;
case CATEGORY_DIR:
updataRows = db.update("Category", values, selection, selectionArgs);
break;
case CATEGORY_ITEM:
String categoryId = uri.getPathSegments().get(1);
updataRows = db.update("Category", values, selection, selectionArgs);
break;
}
return updataRows;
}
}
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.fourteen.MainActivity" >
<Button
android:id="@+id/btnCreateTable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="140dp"
android:text="创建数据库" />
<Button
android:id="@+id/btnUpdatePrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/btnCreateTable"
android:layout_below="@+id/btnCreateTable"
android:layout_marginTop="62dp"
android:text="更新价格" />
<Button
android:id="@+id/btnDeleteData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/btnUpdatePrice"
android:layout_below="@+id/btnUpdatePrice"
android:layout_marginTop="40dp"
android:text="删除数据" />
<Button
android:id="@+id/btnQueryData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/btnDeleteData"
android:text="查询数据" />
<Button
android:id="@+id/btnBeginTransaction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btnCreateTable"
android:layout_alignParentTop="true"
android:layout_marginTop="66dp"
android:text="开始事务" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.fourteen" android:versionCode="1" android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" />
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider android:name=".DatabaseProvider" android:authorities="com.example.fourteen.provider" >
</provider>
</application>
</manifest>