下午,无聊想着学学安卓数据库,所以就做了个简单的项目,话不多说,给大家贴上代码。菜鸟一个,有大佬麻烦指点一点。
这个文章也算给自己做个笔记。
首先,是新建MyDatabaseHelper类继承自SQLiteOpenHeper
package com.example.asus.sqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
public class MyDatabaseHelper extends SQLiteOpenHelper {
public static final String CREATE_BOOK = "create table Book("
+"id integer primary key autoincrement,"
+"author text,"
+"price real,"
+"pages interger,"
+"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){
db.execSQL("drop table if exists Book");
db.execSQL("drop table if exists Category");
onCreate(db);
}
}
接下来,是界面xml的设计,这里我做了两个界面,一个是输入界面,一个是查询界面。但是,同时,也在drawable文件下写了个圆角的输出输入框的xml文件。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
android:textSize="20dp"
/>
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edit"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="author"
android:textSize="20dp"
/>
<EditText
android:id="@+id/author"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edit"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pages"
android:textSize="20dp"
/>
<EditText
android:id="@+id/pages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edit"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="price"
android:textSize="20dp"
/>
<EditText
android:id="@+id/price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edit"/>
<Button
android:id="@+id/create_database"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create database"/>
<Button
android:id="@+id/add_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add data"/>
<Button
android:id="@+id/query2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="query"
android:textSize="20dp"
/>
</LinearLayout>
activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditText
android:id="@+id/sr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Please set name"
android:textSize="20dp"
/>
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edit"
android:textSize="20dp"
/>
<TextView
android:id="@+id/author"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edit"
android:textSize="20dp"
/>
<TextView
android:id="@+id/pages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edit"
android:textSize="20dp"
/>
<TextView
android:id="@+id/price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edit"
android:textSize="20dp"
/>
<Button
android:id="@+id/query"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="query"
android:textSize="20dp"
android:layout_gravity="center"
android:background="@drawable/edit"
/>
</LinearLayout>
edit.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 背景颜色 -->
<solid android:color="#E8F5FF"/>
<!-- 边框的宽度和颜色 -->
<stroke android:width="1dip" android:color="#DBD9D9"/>
<!-- 设置弧形的半径 -->
<corners android:radius="20dp"/>
<!-- padding:Button 里面的文字与Button边界的间隔 -->
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"/>
</shape>
然后,就是重头戏了,我做了一个建表,添加数据,还有查询的功能。
MainActivity.java
package com.example.asus.sqlite;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private MyDatabaseHelper dbHelper;
EditText one;
EditText second;
EditText three;
EditText four;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,2);
one = (EditText)findViewById(R.id.name);
second = (EditText)findViewById(R.id.author);
three = (EditText)findViewById(R.id.pages);
four = (EditText)findViewById(R.id.price);
Button craeteDatabase = (Button) findViewById(R.id.create_database);
craeteDatabase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dbHelper.getWritableDatabase();
}
});
Button addData = (Button)findViewById(R.id.add_data);
addData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
String s1 = one.getText().toString();
String s2 = second.getText().toString();
String s3 = three.getText().toString();
String s4 = four.getText().toString();
int ss3 = Integer.valueOf(s3).intValue();
Double ss4 = Double.valueOf(s4).doubleValue();
values.put("name",s1);
values.put("author",s2);
values.put("pages",ss3);
values.put("price",ss4);
db.insert("Book",null,values);
}
});
Button query = (Button) findViewById(R.id.query2);
query.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
});
}
}
Main2Activity.java
package com.example.asus.sqlite;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentUris;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Main2Activity extends AppCompatActivity {
private MyDatabaseHelper dbHelper;
private TextView one;
private TextView second;
private TextView three;
private TextView four;
private EditText n;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,2);
Button queryButton = (Button) findViewById(R.id.query);
one = (TextView) findViewById(R.id.name);
second = (TextView) findViewById(R.id.author);
three = (TextView) findViewById(R.id.pages);
four = (TextView) findViewById(R.id.price);
n = (EditText)findViewById(R.id.sr);
queryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name2 = n.getText().toString();
String name;
String author;
SQLiteDatabase db = dbHelper.getWritableDatabase();
//查询
Cursor cursor = db.query("Book",null,null,null,null,null,null);
if(cursor.moveToFirst()){
do{
//打印
name = cursor.getString(cursor.getColumnIndex("name"));
author = cursor.getString(cursor.getColumnIndex("author"));
int pages = cursor.getInt(cursor.getColumnIndex("pages"));
String pages2 = String.valueOf(pages);
double price = cursor.getDouble(cursor.getColumnIndex("price"));
String price2 = String.valueOf(price);
if(name2.equals(name)){
one.setText(name);
second.setText(author);
three.setText(pages2);
four.setText(price2);
}
}while (cursor.moveToNext());
}
cursor.close();
}
});
}
}
最终的运行结果为: