SQL基础要求

# 本地数据库 SQLite

通过sqlite ,可将复杂数据存入设备本地,来执行sql 检索数据。


# 创建数据库

语法 SQLiteDB(name,version,initsqls);

参数

  • 参数 name |必填string类型 : 数据库的名称
  • 参数 version |必填number类型 : 数据库的版本
  • 参数 initsqls |必填table类型 : 数据库创建时候 需要执行的sql语句,一般用来创建表。
    • 如:{'sql语句','sql语句'}
-- 案例
    local sqlinit = {
        "create table user(id INTEGER primary key AUTOINCREMENT,uname varchar(50),age int)",
        "create table card(id INTEGER primary key AUTOINCREMENT,uname varchar(50),age int)",
    }

    -- 创建一个数据库 名称 123,版本号为1 ,并在创建时执行了 sqlinit table ,创建了user,card 表
    local db = SQLiteDB("123",1,sqlinit)
1
2
3
4
5
6
7
8

# 删除数据库

语法 db:delect();

参数

  • 参数 无
-- 案例
    -- 创建一个数据库 名称 123,版本号为1 
    local db = SQLiteDB("123",1,nil)

    db.delect(); -- 删除 
1
2
3
4
5

# 获取可读写数据库

语法 db:getWritableDatabase();

参数

  • 参数 无
-- 案例
    -- 创建一个数据库 名称 123,版本号为1 
    local db = SQLiteDB("123",1,nil)

    local dbw = db.getWritableDatabase(); -- 获取可读写的数据库
    -- dbw 对象即为可读写的数据库操作对象,我们可以通过这个对象,增删改查数据库中的表
1
2
3
4
5
6

# 关闭数据库连接

语法 db:close();

我们在不使用数据库的时候尽量去关闭它。

等再次使用的时候,使用getWritableDatabase()方法,该方法可以拿到可读写数据库起打开数据库连接的作用

参数

  • 参数 无
-- 案例
    -- 创建一个数据库 名称 123,版本号为1 
    local db = SQLiteDB("123",1,nil)

    db:close(); -- 关闭数据库

    local dbw = db.getWritableDatabase(); -- 获取可读写的数据库(可重新打开数据库)
    -- dbw 对象即为可读写的数据库操作对象,我们可以通过这个对象,增删改查数据库中的表
1
2
3
4
5
6
7
8

# 执行SQL语句(增删改-表)

语法 db:execSQL("sqlstr");

参数

  • 参数 sqlstr |必填string类型 : 数据库语句
  • 例如:增加数据:"insert into user (uname,age) values ('张三',18)"
-- 案例
local sqlinit = {
    "create table user(id INTEGER primary key AUTOINCREMENT,uname varchar(50),age int)",
    "create table card(id INTEGER primary key AUTOINCREMENT,cardnumber varchar(50),password varchar(100))",
}

-- 创建一个数据库 名称 123,版本号为1 ,并在创建时执行了 sqlinit table ,创建了user,card 表
local db = SQLiteDB("123",1,sqlinit)

local dbw = db:getWritableDatabase();

dbw:execSQL("insert into user (uname,age) values ('张三',18)")
1
2
3
4
5
6
7
8
9
10
11
12

# 查询表数据

语法 db:rawQuery("sqlstr");

查询语句 会返回一个 cursor ,我们可以通过cursor 来迭代每一条数据。

参数

  • 参数 sqlstr |必填string类型 : sql 查询语句
  • 例如:增加数据:"select * from user"
-- 案例
local sqlinit = {
    "create table user(id INTEGER primary key AUTOINCREMENT,uname varchar(50),age int)",
    "create table card(id INTEGER primary key AUTOINCREMENT,cardnumber varchar(50),password varchar(100))",
}

-- 创建一个数据库 名称 123,版本号为1 ,并在创建时执行了 sqlinit table ,创建了user,card 表
local db = SQLiteDB("123",1,sqlinit)

local dbw = db:getWritableDatabase();

-- cursor 是个数据库游标,其所支持的所有方法见补充
cursor = dbw:rawQuery("select * from user",nil);
-- 移动游标到第一行
cursor:moveToFirst();
-- 迭代数据库 -- 游标已经到最后一行之后了
while not cursor:isAfterLast() do
    local tid = cursor:getInt(0);
    print("自增id为:"..tid);
    local tName = cursor:getString(1);
    print("姓名为:"..tName);
    local tAge= cursor:getInt(2);
    print("年龄为:"..tAge);
    
    --将游标移到下一行
    cursor:moveToNext();
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

cursor 对象的补充 由于数据库检索,可能存在成百上千万的检索数据 ,因此我们不能直接返回检索答案,而是采用 cursor 游标的方式。

cursor android.database.Cursor 中的类,我们可以直接访问其所有的公共方法

Cursor 支持的方法预览

# MYSQL 官方云端数据库

作为节点精灵的开发者,我们为其提供免费的 云端数据库MySQL

开发者可前往后台申请开通数据库

# 创建节点云 mysql

# 连接数据库

语法 MySql(args)

我们可以通过此方法 快速的连接云端数据库

节点云 MySql 参数

参数

  • args |必填table类型 : 数据库参数
    • ip:云端数据库的ip
    • port:数据库端口(节点云mysql 端口为3307,自建mysql一般为3306)
    • db:数据库的名称
    • user:数据库的用户名
    • password:数据库的密码
    • characterEncoding: 数据库的编码(可不填,默认utf-8)
-- 案例
local params= {
    ip = "8.140.162.237",--数据库ip地址
    port = 3307,--数据库端口
    db = "db_dev_1",--数据库名称
    user = "2404",--数据库用户名
    password = "123",--数据库密码
    characterEncoding = "utf-8"--数据库字符编码
}

conn = MySql(params);

if conn then
    print("连接成功了")
    conn:close();
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 关闭数据库

语法 conn:close()

我们在使用数据库后一定要记得,主动关闭数据库连接。减少服务器资源的占用

最大连接数

  • 单个最大连接数不得超过100
  • 因此开发者要及时 释放连接

参数

  • 无参数
-- 案例
local params= {
    ip = "8.140.162.237",--数据库ip地址
    port = 3307,--数据库端口
    db = "db_dev_1",--数据库名称
    user = "2404",--数据库用户名
    password = "123",--数据库密码
    characterEncoding = "utf-8"--数据库字符编码
}

conn = MySql(params);

if conn then
    print("连接成功了")
    conn:close();
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 执行SQL语句(增删改)

语法 conn:createStatement():executeUpdate(sql)

我们可以通过此方法执行 传统 SQL语句的 增删改

参数

-- 案例
local params= {
    ip = "8.140.162.237", --数据库ip地址
    port = 3307,--数据库端口
    db = "db_dev_1",--数据库名称
    user = "240417809",--数据库用户名
    password = "1122334",--数据库密码
    characterEncoding = "utf-8"--数据库字符编码
}

conn = MySql(params);

if conn then
    print("连接成功了")
    stmt = conn:createStatement();
    sql ="create table user (uid char(10) primary key,name char(50),age int)" -- 创建表
    -- sql ="drop table user"  -- 删除表
    -- sql = "insert into user values ('1','张三',10) " -- 插入语句
    -- sql = "update user set age = 100 WHERE name = '张三'" --更新语句
    -- sql = "delete from user where name = '张三'" --删除语句
    re = stmt:executeUpdate(sql); -- 执行 sql 语句
    conn:close();
end

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# 执行SQL语句(查询)

语法 conn:createStatement():executeQuery(sql)

我们可以通过此方法执行 传统 SQL语句的 增删改

参数

-- 案例

local params= {
    ip = "8.140.162.237",--数据库ip地址
    port = 3307,--数据库端口
    db = "db_dev_1",--数据库名称
    user = "240417809",--数据库用户名
    password = "1122334",--数据库密码
    characterEncoding = "utf-8"--数据库字符编码
}

conn = MySql(params);

if conn then
    print("连接成功了")
    stmt = conn:createStatement();
    sql = "select * from user "
    r = stmt:executeQuery(sql)
    while r:next() do 
        local cid =  r:getString(1);
        local cName =  r:getString(2);
        local cAge =  r:getInt(3);
        print(cid..","..cName..","..cAge)
    end
    conn:close();
end

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# 更多JDBC API

我们只演示了 普通的增删改查功能, 更复杂的操作,如 事物 ,锁 等操作相关的API 请各位开发者查询 Java.sql 的文档,所有API 均支持

Java sql (opens new window)

文档中.和:的区别

  • 由于java 中访问对象方法用.即可,但lua 中访问对象必须使用:
  • 因此 我们在lua中记得使用“:”来访问对象的属性方法哦