package com.example.sqlitetest;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
public class MyDatavaseHelper extends SQLiteOpenHelper {
private static final String CREATE_BOOK = "create table Book(\n" +
" id integer primary key autoincrement,\n" +
" author text,\n" +
" price real,\n" +
" pages integer,\n" +
" name text\n" +
" )";
// private static final String CREATE_CATEGORY = "create table Category(\n" +
// " id integer primary key autoincrement,\n" +
// " category_name text,\n" +
// " category_code integer\n" +
// " )\n";
private Context mContext;
public MyDatavaseHelper(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, "建表成功!", Toast.LENGTH_LONG).show();
}
@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);
}
}
<?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="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical"
tools:context="com.example.sqlitetest.MainActivity">
<Button
android:id="@+id/create_database"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="创建数据库"
/>
<Button
android:id="@+id/add_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="添加数据"
/>
<Button
android:id="@+id/update_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="修改数据"
/>
<Button
android:id="@+id/delete_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="删除数据"
/>
<Button
android:id="@+id/query_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="查询数据"
/>
</LinearLayout>
package com.example.sqlitetest;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.icu.text.LocaleDisplayNames;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.sql.SQLData;
public class MainActivity extends AppCompatActivity {
private MyDatavaseHelper dbHelper;
private static final String TAG = "MainActivity";
Context context = getBaseContext();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new MyDatavaseHelper(this, "BookStore.db", null, 1);
Button createDatabase = (Button) findViewById(R.id.create_database);
createDatabase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper.getWritableDatabase();
}
});
//添加数据
Button addData = (Button) findViewById(R.id.add_data);
addData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
//第二种方法加入数据
db.execSQL("insert into Book(name,author,pages,price) values(?,?,?,?)",
new String[]{
"oiujhk", "jhjh", "454", "165"
}
);
//第二种方法加入数据
ContentValues values = new ContentValues();
//开始组装第一条数据
values.put("name", "kkk");
values.put("author", "dan dd");
values.put("pages", 454);
values.put("price", 16.96);
db.insert("Book", null, values);//插入第一条数据
values.clear();
// Toast.makeText(context,"增加数据成功",Toast.LENGTH_LONG).show();
//开始第二条数据
values.put("name", "反而费");
values.put("author", "dan dd");
values.put("pages", 555);
values.put("price", 1665);
db.insert("Book", null, values);//插入第二条数据
Log.d(TAG, "添加成功!");
}
});
/**
* 修改数据
*/
Button updData = (Button) findViewById(R.id.update_data);
updData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
//开始组装第一条数据
values.put("price", 45);
db.update("Book", values, "name= ?", new String[]{
"kkk"
});//修改第一条数据
Log.d(TAG, "数据修改成功");
}
});
/**
* 删除数据
*/
Button delData = (Button) findViewById(R.id.delete_data);
delData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete("Book", "pages>?", new String[]{
"500"
});
Log.d(TAG, "删除成功");
}
});
/**
* 查询数据
*/
Button queryData = (Button) findViewById(R.id.query_data);
queryData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
//第二种查询方法
Cursor cursor1 = db.rawQuery("select * from Book", null);
//查询Book中的所有的数据
Cursor cursor = db.query("Book", null, null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
//遍历cursor对象
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(TAG, "name:" + name);
Log.d(TAG, "author:" + author);
Log.d(TAG, "pages:" + pages);
Log.d(TAG, "price:" + price);
} while (cursor.moveToNext());
}
cursor.close();
Log.d(TAG, "查询成功");
}
});
}
}