SQLite的简单使用
工具:
- android studio
-
SQLite 3
至于以上两种工具的下载以及安装,可以自行百度
首先打开android studio 创建一个新项目
以下模板中随便一个都行,在这里我选择了Empty Activity,取名任意。
先运行一下结果是这样的:
就是一个空白页,接下来我们让他实现从SQLite读取数据的功能,代码如下:
MainActivity.java
package com.example.sqlitetestdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
static final String db_name = "testDatabase";
static final String tb_name = "testTable";
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//数据库操作
db = openOrCreateDatabase(db_name, Context.MODE_PRIVATE, null);
String createTable = "CREATE TABLE IF NOT EXISTS " +
tb_name +
"(name VARCHAR(32), " +
"birth_and_age VARCHAR(60), " +
"job_date VARCHAR(60), " +
"dang_date VARCHAR(60), " +
"message VARCHAR(100))";
db.execSQL(createTable);
Cursor c = db.rawQuery("SELECT * FROM "+tb_name,null);
//若数据库表中没有数据,则新增一条数据
if(c.getCount() == 0){
db.execSQL("INSERT INTO testTable VALUES('张三','1962.12出生,53岁,汉族','1984.08参加工作','1983.04中共党员','2011.12任现值')");
c = db.rawQuery("SELECT * FROM "+tb_name,null);
}
if(c.moveToFirst()) {
String str1,str2,str3,str4,str5;
str1 = c.getString(0);
str2 = c.getString(1);
str3 = c.getString(2);
str4 = c.getString(3);
str5 = c.getString(4);
TextView textView1 = (TextView) findViewById(R.id.textView2);
TextView textView2 = (TextView) findViewById(R.id.textView3);
TextView textView3 = (TextView) findViewById(R.id.textView4);
TextView textView4 = (TextView) findViewById(R.id.textView5);
TextView textView5 = (TextView) findViewById(R.id.textView6);
textView1.setText(str1);
textView2.setText(str2);
textView3.setText(str3);
textView4.setText(str4);
textView5.setText(str5);
}
db.close();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView4"
android:layout_width="235dp"
android:layout_height="21dp"
android:layout_marginTop="132dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/textView5"
android:layout_width="235dp"
android:layout_height="21dp"
android:layout_marginTop="168dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/textView6"
android:layout_width="235dp"
android:layout_height="21dp"
android:layout_marginStart="2dp"
android:layout_marginLeft="2dp"
android:layout_marginTop="202dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="79dp"
android:layout_height="21dp"
android:layout_marginTop="62dp"
android:text="姓名"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/textView2"
android:layout_width="87dp"
android:layout_height="21dp"
android:layout_marginStart="97dp"
android:layout_marginLeft="97dp"
android:layout_marginTop="62dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/textView3"
android:layout_width="235dp"
android:layout_height="21dp"
android:layout_marginStart="2dp"
android:layout_marginLeft="2dp"
android:layout_marginTop="96dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
一个activity.java对应一个activity.xml文件,相当于一个控制器对应一个视图,这有些类似于spring mvc的逻辑。
这是视图:
最后点击上图的三角运行,运行结果如下图:
版权声明:本文为a1106103430原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。