SQLite簡單資料庫

2021-08-20 17:43:10 字數 3297 閱讀 9822

通過觀察可以發現,不管是聊天列表還是**列表,有乙個共性:

資料量大和資料結構複雜

那麼為什麼我們要用sqlite,有這麼幾個原因

sqlite常用的幾種資料型別為text文字型,integer整型,real浮點型,建**式要用規定的格式,如下

create table product

建立資料庫和資料表的步驟

1.新建類繼承sqliteopenhelper

2.實現構造方法

3.重寫oncreate方法

4.重寫onupgrade方法

5.例項sqliteopenhelper的子類物件

我們簡單的舉個例子

import android.content.context;

import android.database.sqlite.sqlitedatabase;

import android.database.sqlite.sqliteopenhelper;

/** * created by asus on 2018/6/13.

*/public

class

mysqlitehelper

extends

sqliteopenhelper

@override

public

void

oncreate(sqlitedatabase db)

@override

public

void

onupgrade(sqlitedatabase db, int oldversion, int newversion) }

sqliteopenhelper是乙個資料庫輔助類,用來管理資料庫的建立和版本。通過繼承這個類,實現他的一些方法對資料庫進行一些操作。

這裡要記住,oncreate方法只會在第一次建立執行時執行,之後不再執行。

首先,我們頂乙個簡單的布局:

<?xml version="1.0" encoding="utf-8"?>

xmlns:android=""

xmlns:tools=""

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textsize="30sp"

android:textcolor="#123456"

android:id="@+id/ed1_sql"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textsize="30sp"

android:textcolor="#123456"

android:id="@+id/ed2_sql"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="增加"

android:id="@+id/btn1_sql"

android:textsize="30sp"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="修改"

android:id="@+id/btn2_sql"

android:textsize="30sp"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="刪除"

android:id="@+id/btn3_sql"

android:textsize="30sp"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="查詢"

android:id="@+id/btn4_sql"

android:textsize="30sp"/>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:id="@+id/list_sql">

listview>

linearlayout>

這裡我們新增了乙個listview,用來將查詢出來的資料顯示在listview中,資料表的建立詳細見上面的**,我們在這分別建立四個方法用來實現功能

private

void

addstudent()

這個方法用來新增

private

void

updatestudent() );

}

這個是修改的方法

private

void

delstudent() );

}

刪除方法

private

void

selectstudent() while (cursor.movetonext());

sqladapter adapter=new sqladapter(sqlactivity.this,studentlist);

listview.setadapter(adapter);

}

以及查詢方法

在這裡,我們主要來看看下面這段**

mysqlitehelper mysqlitehelper = new mysqlitehelper(this, "student_db", null, 1);

sqlitedatabase db = mysqlitehelper.getwritabledatabase();

通過建立mysqlitehelper物件,用sqlitedatabase呼叫getwritabledatabase()方法來實現手動輸入新增

簡單資料庫程式設計

1 修改配置檔案 web.config 2 建立鏈結類 connection.cs using system.data.sqlclient 新增名字空間 public static sqlconnection getconnection return new sqlconnection system...

php簡單資料庫練習

consolelog include test 接收使用者提交資料,並儲存到資料庫中 if post elseelse else else 登陸頁面時,從資料庫讀取 連線資料庫操作 servername localhost username root passowrd root 建立連線 conn ...

簡單資料庫操作問題

簡單資料庫操作問題 delphi windows sdk api 請問操作access資料庫時 如果資料庫是 表 word 字段 name 字段 pass 讀access把所有的name 加入combobox控制項中 然後 點裡面的內容就在edit1.text中顯示對應的pass內容 1。with ...