軟件見文末
一、連接步驟
前提是先安裝好sqllite---->無腦式next安裝
1、打開Android studio 創(chuàng)建一對Activity,
2、書寫相關(guān)代碼
// 在 StudentActivity.java
package com.example.myapplication01;
import androidx.appcompat.app.AppCompatActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class StudentActivity extends AppCompatActivity {
private EditText bianhao;
private EditText name;
private EditText age;
private EditText czbianhao;
private EditText czname;
private TextView czresult;
private SQLiteDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student);
bianhao = findViewById(R.id.bianhao);
name = findViewById(R.id.name);
age = findViewById(R.id.age);
czbianhao = findViewById(R.id.czbianhao);
czname = findViewById(R.id.czxingming);
czresult = findViewById(R.id.result);
Mydatabase mydatabase = new Mydatabase(StudentActivity.this);
database = mydatabase.getWritableDatabase();
}
public void insert(View view) {
String sql1 = "select * from user where 編號=?";
Cursor cursor = database.rawQuery(sql1, new String[]{bianhao.getText().toString()});
if (cursor.getCount() == 0) {
String sql = "insert into user(編號,姓名,年齡)values(?,?,?)";
database.execSQL(sql, new Object[]{Integer.parseInt(bianhao.getText().toString()), name.getText().toString(),
Integer.parseInt(age.getText().toString())});
Toast.makeText(getApplicationContext(), "已成功添加!!!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "數(shù)據(jù)已存在!!!", Toast.LENGTH_SHORT).show();
bianhao.setText("");
bianhao.requestFocus();
}
}
//方法二
/*String sql1="select * from user where 編號=?";
Cursor cursor = database.rawQuery(sql1,new String[]{bianhao.getText().toString()});
if(cursor.getCount()==0){
ContentValues contentValues = new ContentValues();
contentValues.put("編號",Integer.parseInt(bianhao.getText().toString()));
contentValues.put("姓名",name.getText().toString());
contentValues.put("年齡",Integer.parseInt(age.getText().toString()));
Toast.makeText(getApplicationContext(),"已成功添加!!!",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getApplicationContext(),"數(shù)據(jù)已存在!!!",Toast.LENGTH_SHORT).show();
bianhao.setText("");
bianhao.requestFocus();
}*/
public void delete(View view){
String sql = "delete from user where 編號=?";
database.execSQL(sql,new Object[]{Integer.parseInt(bianhao.getText().toString())});
Toast.makeText(getApplicationContext(),"數(shù)據(jù)已刪除!!!",Toast.LENGTH_SHORT).show();
}
public void update(View view){
String sql = "update user set 姓名=?,年齡=? where 編號=?";
database.execSQL(sql,new Object[]{name.getText().toString(),Integer.parseInt(age.getText().toString()),
Integer.parseInt(bianhao.getText().toString())});
Toast.makeText(getApplicationContext(),"數(shù)據(jù)已更新!!!",Toast.LENGTH_SHORT).show();
}
public void findbianhao(View view){
String sql="select * from user where 編號=?";
Cursor cursor = database.rawQuery(sql,new String[]{czbianhao.getText().toString()});
if(cursor.moveToNext()){
int bianhao1 = cursor.getInt(cursor.getColumnIndex("編號"));
String name1 = cursor.getString(cursor.getColumnIndex("姓名"));
int age1 = cursor.getInt(cursor.getColumnIndex("年齡"));
czresult.setText("查找結(jié)果->編號: "+bianhao1+"\t姓名:"+name1+"\t年齡:"+age1);
}else {
Toast.makeText(getApplicationContext(),"無記錄!!!",Toast.LENGTH_SHORT).show();
czresult.setText("");
}
}
public void findname(View view){
String sql="select * from user where 姓名=?";
Cursor cursor = database.rawQuery(sql,new String[]{czname.getText().toString()});
if(cursor.moveToNext()){
int bianhao2 = cursor.getInt(cursor.getColumnIndex("編號"));
String name2 = cursor.getString(cursor.getColumnIndex("姓名"));
int age2 = cursor.getInt(cursor.getColumnIndex("年齡"));
czresult.setText("查找結(jié)果->編號: "+bianhao2+"\t姓名:"+name2+"\t年齡:"+age2);
}else {
Toast.makeText(getApplicationContext(),"無記錄!!!",Toast.LENGTH_SHORT).show();
czresult.setText("");
}
}
}
<!--在 activity_student_xml中-->
<?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="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"
android:background="@drawable/bg6"
android:orientation="vertical"
android:gravity="center"
tools:context="com.example.myapplication01.StudentActivity">
<TextView
android:textSize="40dp"
android:textStyle="bold"
android:textColor="@android:color/holo_red_dark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="學(xué)生信息管理系統(tǒng)"
android:layout_marginBottom="36dp"
android:id="@+id/textView11" />
<LinearLayout
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_marginBottom="20dp"
android:layout_height="wrap_content">
<TextView
android:textColor="@android:color/black"
android:textSize="25dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="編 號"
android:id="@+id/textView" />
<EditText
android:textColor="@android:color/black"
android:textSize="25dp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bianhao" />
</LinearLayout>
<LinearLayout
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:orientation="horizontal"
android:layout_marginBottom="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="@android:color/black"
android:textSize="25dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓 名"
android:id="@+id/textView2" />
<EditText
android:textColor="@android:color/black"
android:textSize="25dp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name" />
</LinearLayout>
<LinearLayout
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_marginBottom="20dp"
android:layout_height="wrap_content">
<TextView
android:textColor="@android:color/black"
android:textSize="25dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年 齡"
android:id="@+id/textView3" />
<EditText
android:textColor="@android:color/black"
android:textSize="25dp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:maxLength="3"
android:id="@+id/age"
/>
</LinearLayout>
<LinearLayout
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_marginBottom="20dp"
android:layout_height="wrap_content">
<TextView
android:textColor="@android:color/black"
android:textSize="25dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查找的編號"
android:id="@+id/textView4" />
<EditText
android:textColor="@android:color/black"
android:textSize="25dp"
android:textStyle="bold"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:id="@+id/czbianhao" />
<Button
android:onClick="findbianhao"
android:textSize="20dp"
android:textStyle="bold"
android:background="@drawable/buttonpress2"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查 找"
android:id="@+id/buttonczbianhao" />
</LinearLayout>
<LinearLayout
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_marginBottom="20dp"
android:layout_height="wrap_content">
<TextView
android:textColor="@android:color/black"
android:textSize="25dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查找的姓名"
android:id="@+id/textView5" />
<EditText
android:textColor="@android:color/black"
android:textSize="25dp"
android:textStyle="bold"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:id="@+id/czxingming" />
<Button
android:onClick="findname"
android:textSize="20dp"
android:textStyle="bold"
android:background="@drawable/buttonpress2"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查 找"
android:id="@+id/buttonczxingming" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:gravity="center"
android:layout_height="wrap_content">
<Button
android:onClick="insert"
android:textSize="20dp"
android:textStyle="bold"
android:background="@drawable/buttonpress2"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添 加"
android:id="@+id/btnadd" />
<Button
android:onClick="delete"
android:textSize="20dp"
android:textStyle="bold"
android:background="@drawable/buttonpress2"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="刪 除"
android:layout_marginLeft="30dp"
android:id="@+id/btndel" />
<Button
android:onClick="update"
android:textSize="20dp"
android:textStyle="bold"
android:background="@drawable/buttonpress2"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修 改"
android:layout_marginLeft="30dp"
android:id="@+id/btnupdate" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textSize="25dp"
android:textStyle="bold"
android:textColor="@android:color/holo_red_dark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查找結(jié)果:"
android:id="@+id/result1" />
<TextView
android:textSize="25dp"
android:textStyle="bold"
android:textColor="@android:color/holo_red_dark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/result" />
</LinearLayout>
</LinearLayout>
提示:.xml有些資源需要用自己有的,否者有可能會報(bào)錯(cuò)?。。?!
3、創(chuàng)建一個(gè)Mydatabase的一個(gè)Java類。
//在Mydatanase.java中書寫
package com.example.myapplication01;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class Mydatabase extends SQLiteOpenHelper {
static String name = "www.db";
static int version = 1;
public Mydatabase(Context context){
super(context,name,null,version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table user(編號 Integer,姓名 varchar(10),年齡 Integer)";
//逗號是英文的
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
4、運(yùn)行
5、啟動(dòng)monitor連接數(shù)據(jù)庫
1、打開SDK后,查看SDK路徑
2、在SDK路徑下右鍵鼠標(biāo)運(yùn)行命令行,輸入命令monitor,即可啟動(dòng)Android monitor Device如下圖所示:
執(zhí)行完monitor,正常情況下會直接跳轉(zhuǎn)出以下界面以上是正常不出錯(cuò)的連接步驟
6、使用模擬器運(yùn)行的界面進(jìn)行操作可能出現(xiàn)的問題
1、執(zhí)行后前臺顯示成功,數(shù)據(jù)庫里面的refresh沒反應(yīng)的問題
解決方案:
打開Android studio,在如下圖所示的地方可以打開data文件夾
打開sqllite
二、常見錯(cuò)誤
0、在運(yùn)行monitor時(shí)跳轉(zhuǎn)頁面時(shí)有可能會彈出
1、以上這是由于有端口號沖突問題,如果點(diǎn)擊ok以后不是這個(gè)界面并且右邊的data能點(diǎn)開,那就問題不大,可以忽略以下操作
2、 如果是這個(gè)界面,并且data也點(diǎn)不開要進(jìn)行的操作
將端口號修改一下:
data打不開是由于權(quán)限不夠需要進(jìn)行以下操作:
執(zhí)行以下操作之前需要配置platform-tools環(huán)境變量
1、找到這個(gè)目錄
2、打開高級設(shè)置
配好直接點(diǎn)三次確定退出
3、打開cmd輸入adb shell,顯示以下轉(zhuǎn)態(tài)就是可以了。
3、關(guān)于/system/bin/sh: su: not found的解決辦法
c:\user\zg>adb shell
generic_x86:/ $ su
/system/bin/sh: su: not found
原因是
Android Studio帶(Google Play)的模擬器無法獲得root權(quán)限安裝
該換成為帶(Google APIs)的模擬器即可,如下
4、解決無法打開data文件夾,原因是權(quán)限不夠,需要設(shè)置權(quán)限
可以一層一層的給權(quán)限
C:\Users\zg>adb shell
generic_x86_64:/ $ su
generic_x86_64:/ # chmod 777 /data
generic_x86_64:/ # exit
generic_x86_64:/ $ su
generic_x86_64:/ # chmod 777 /data/data
generic_x86_64:/ # exit
generic_x86_64:/ $ exit
結(jié)束以上操作,退出Android device monitor,重新執(zhí)行以下命令
彈出這個(gè)界面(之前的爆紅就消失了)
左邊依舊是問號,這時(shí)執(zhí)行以下操作即可
1、首先先獲取root權(quán)限
打開cmd執(zhí)行
2、在返回Android device monitor中執(zhí)行以下操作
文章來源:http://www.zghlxwxcb.cn/news/detail-764688.html
相關(guān)資料
鏈接:https://pan.baidu.com/s/14TrrJlCP7b5gxQPC3PpQlg?pwd=nduf
提取碼:nduf文章來源地址http://www.zghlxwxcb.cn/news/detail-764688.html
到了這里,關(guān)于Android Studio + sqllite 數(shù)據(jù)庫連接的步驟以及常見問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!