SQLite數據庫的常用操作:
create table if not exists 表名(字段1 類型(長度),字段2 類型(長度),...)// 建表 drop table if exists 表名//刪除表 insert into 表名 (字段1,字段2,字段3 ...) values (值1,值2,值3 ...);//增 insert into 目標數據表 select * from 源數據表; delete from 表名 where 條件表達式// 刪 update 表名 set 字段1=值1,字段2=值2... where 條件表達式// 改 select * from 表名 where 條件表達式//查
簡單小實例:(注:“ALT+ENTER”組合鍵導入class)
非常簡單的例子,只用到了一個界面
1、首先先創建一個DBHelper類(DBOpenHelper.java)
在這里會執行建庫、建表的操作
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(Context context,String name, CursorFactory factory,
int version){
super(context, name, factory, version);
}
@Override
//首次創建數據庫的時候調用,一般可以執行建庫,建表的操作
//Sqlite沒有單獨的布爾存儲類型,它使用INTEGER作為存儲類型,0為false,1為true
public void onCreate(SQLiteDatabase db){
//user table
db.execSQL("create table if not exists user(name text not null,pwd text not null)");
}
@Override//當數據庫的版本發生變化時,會自動執行
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
}
}
2、進入登錄界面
在點擊登錄按鈕時,會去數據庫里面進行查詢,判斷賬號是否存在(Query查詢范例)
//判斷賬號/密碼是否輸入正確
public void OnMyLoginClick(View v){
EditText name=(EditText)findViewById(R.id.editText2);
EditText pwd=(EditText)findViewById(R.id.editText4);
//調用DBOpenHelper (user.db是創建的數據庫的名稱)
DBOpenHelper helper = new DBOpenHelper(this,"test.db",null,1);
SQLiteDatabase db = helper.getWritableDatabase();
//根據畫面上輸入的賬號/密碼去數據庫中進行查詢(user是表名)
Cursor c = db.query("user",null,"name=? and pwd=?",new String[]{name.getText().toString(),pwd.getText().toString()},null,null,null);
//如果有查詢到數據
if(c!=null && c.getCount() >= 1){
Toast.makeText(this, "輸入正確!", Toast.LENGTH_SHORT).show();
//可以把查詢出來的值打印出來在后台顯示/查看
String[] cols = c.getColumnNames();
while(c.moveToNext()){
for(String ColumnName:cols){
Log.i("info",ColumnName+":"+c.getString(c.getColumnIndex(ColumnName)));
}
}
c.close();
db.close();
this.finish();
}
//如果沒有查詢到數據
else{
Toast.makeText(this, "輸入錯誤!", Toast.LENGTH_SHORT).show();
}
}
3、如果賬號不存在,則需要去注冊一個新賬號(Insert新增范例)
//添加
public void OnMyRegistClick(View v){
EditText name=(EditText)findViewById(R.id.editText2);
EditText pwd=(EditText)findViewById(R.id.editText4);
//對用戶輸入的值的格式進行判斷的處理...
//調用DBOpenHelper
DBOpenHelper helper = new DBOpenHelper(this,".db",null,1);
SQLiteDatabase db = helper.getWritableDatabase();
//根據畫面上輸入的賬號去數據庫中進行查詢
Cursor c = db.query("user",null,"name=?",new String[]{name.getText().toString()},null,null,null);
//如果有查詢到數據,則說明賬號已存在
if(c!=null && c.getCount() >= 1){
Toast.makeText(this, "該用戶已存在", Toast.LENGTH_SHORT).show();
String[] cols = c.getColumnNames();
while(c.moveToNext()){
for(String ColumnName:cols){
Log.i("info",ColumnName+":"+c.getString(c.getColumnIndex(ColumnName)));
}
}
c.close();
}
//如果沒有查詢到數據,則往數據庫中insert一筆數據
else{
//insert data
ContentValues values= new ContentValues();
values.put("name",name.getText().toString());
values.put("pwd",pwd.getText().toString());
long rowid = db.insert("user",null,values);
Toast.makeText(this, "注冊成功", Toast.LENGTH_SHORT).show();//提示信息
//this.finish();
}
db.close();
}
界面代碼;
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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.tzk.sqliteloginapplication.MainActivity"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPersonName" android:text="Name" android:ems="10" android:id="@+id/editText2" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="16dp" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPassword" android:ems="10" android:layout_below="@+id/editText2" android:layout_centerHorizontal="true" android:layout_marginTop="13dp" android:id="@+id/editText4" android:text="000000" /> <Button android:text="登錄" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="13dp" android:id="@+id/button" android:layout_below="@+id/editText4" android:layout_alignStart="@+id/editText4" android:onClick="OnMyLoginClick"/> <Button android:text="注冊" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/button" android:layout_toEndOf="@+id/button" android:layout_marginStart="18dp" android:id="@+id/button2" android:onClick="OnMyRegistClick"/> </RelativeLayout>
MainActivity:

package com.tzk.sqliteloginapplication;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//判斷賬號/密碼是否輸入正確
public void OnMyLoginClick(View v){
EditText name=(EditText)findViewById(R.id.editText2);
EditText pwd=(EditText)findViewById(R.id.editText4);
//調用DBOpenHelper (user.db是創建的數據庫的名稱)
DBOpenHelper helper = new DBOpenHelper(this,"test.db",null,1);
SQLiteDatabase db = helper.getWritableDatabase();
//根據畫面上輸入的賬號/密碼去數據庫中進行查詢(user是表名)
Cursor c = db.query("user",null,"name=? and pwd=?",new String[]{name.getText().toString(),pwd.getText().toString()},null,null,null);
//如果有查詢到數據
if(c!=null && c.getCount() >= 1){
Toast.makeText(this, "輸入正確!", Toast.LENGTH_SHORT).show();
//可以把查詢出來的值打印出來在后台顯示/查看
String[] cols = c.getColumnNames();
while(c.moveToNext()){
for(String ColumnName:cols){
Log.i("info",ColumnName+":"+c.getString(c.getColumnIndex(ColumnName)));
}
}
c.close();
db.close();
this.finish();
}
//如果沒有查詢到數據
else{
Toast.makeText(this, "輸入錯誤!", Toast.LENGTH_SHORT).show();
}
}
//添加
public void OnMyRegistClick(View v){
EditText name=(EditText)findViewById(R.id.editText2);
EditText pwd=(EditText)findViewById(R.id.editText4);
//對用戶輸入的值的格式進行判斷的處理...
//調用DBOpenHelper
DBOpenHelper helper = new DBOpenHelper(this,".db",null,1);
SQLiteDatabase db = helper.getWritableDatabase();
//根據畫面上輸入的賬號去數據庫中進行查詢
Cursor c = db.query("user",null,"name=?",new String[]{name.getText().toString()},null,null,null);
//如果有查詢到數據,則說明賬號已存在
if(c!=null && c.getCount() >= 1){
Toast.makeText(this, "該用戶已存在", Toast.LENGTH_SHORT).show();
String[] cols = c.getColumnNames();
while(c.moveToNext()){
for(String ColumnName:cols){
Log.i("info",ColumnName+":"+c.getString(c.getColumnIndex(ColumnName)));
}
}
c.close();
}
//如果沒有查詢到數據,則往數據庫中insert一筆數據
else{
//insert data
ContentValues values= new ContentValues();
values.put("name",name.getText().toString());
values.put("pwd",pwd.getText().toString());
long rowid = db.insert("user",null,values);
Toast.makeText(this, "注冊成功", Toast.LENGTH_SHORT).show();//提示信息
//this.finish();
}
db.close();
}
}