小工具整理
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
(
|
||||
-- 这是一个关于如何使用MySQL Connector查询MySQL数据库的小教程。
|
||||
-- 本教程假设您已经设置了MySQL服务器,安装了MySQL Connector(可在MySQL官网找到),并且具有SQL的基本知识。
|
||||
|
||||
-- 我将在不久的将来创建一个更易于使用的封装。
|
||||
-- 祝您愉快。
|
||||
-- Ofer Zelichover(www.oferz.com)2008-03-08
|
||||
--ySQL Connector下载链接:https://www.mysql.com/products/connector 下载时选择.net版本就行,一路默认安装即可
|
||||
-- 首先加载dotNet程序集。确保字符串指向安装了MySQL Connector DLL文件的位置。
|
||||
dotNet.loadAssembly "C:\\Program Files (x86)\\MySQL\\MySQL Connector NET 8.2.0\\MySql.Data.dll"
|
||||
|
||||
-- 现在定义连接参数。
|
||||
local host = "localhost" -- MySQL服务器的名称
|
||||
local database = "test" -- 要使用的数据库的名称
|
||||
local user = "test" -- 要使用的用户名
|
||||
local password = "test" -- 要使用的密码
|
||||
|
||||
-- 构建连接字符串
|
||||
local connectionString = "Database=" + database + ";Data Source=" + host + ";User Id=" + user + ";Password=" + password
|
||||
|
||||
-- 创建连接对象
|
||||
local DBConnection = dotNetObject "MySql.Data.MySqlClient.MySqlConnection"
|
||||
-- 使用连接字符串设置连接参数。
|
||||
DBConnection.ConnectionString = connectionString
|
||||
-- 打开数据库连接
|
||||
DBConnection.open()
|
||||
|
||||
-- 打印连接状态:1 - 已连接;0 - 未连接。
|
||||
print DBConnection.state.value__
|
||||
|
||||
|
||||
-- 添加两行新记录
|
||||
local cmdObject = DBConnection.CreateCommand()
|
||||
cmdObject.commandText = "INSERT INTO test_table (`name`, `phone`) VALUES ('另一个名字','1234567890'), ('又一个名字','2468101214')"
|
||||
local readerObject = cmdObject.ExecuteReader()
|
||||
-- 打印受影响的行数
|
||||
format "添加了 % 行\n" readerObject.RecordsAffected
|
||||
readerObject.close()
|
||||
|
||||
|
||||
-- 更改表中第二行的值:
|
||||
local cmdObject = DBConnection.CreateCommand()
|
||||
cmdObject.commandText = "UPDATE test_table tt SET tt.name='修改后的名字', tt.phone=78978979 WHERE tt.id=2"
|
||||
local readerObject = cmdObject.ExecuteReader()
|
||||
-- 打印受影响的行数
|
||||
format "修改了 % 行\n" readerObject.RecordsAffected
|
||||
readerObject.close()
|
||||
|
||||
|
||||
-- 查询数据库:
|
||||
-- 首先创建一个命令对象。该对象用于将查询发送到数据库。
|
||||
local cmdObject = DBConnection.CreateCommand()
|
||||
-- 定义要执行的SQL查询:
|
||||
cmdObject.commandText = "SELECT * FROM test_table tt"
|
||||
-- 然后执行命令以创建读取器对象。
|
||||
local readerObject = cmdObject.ExecuteReader()
|
||||
|
||||
-- 打印结果中的字段名:
|
||||
for i = 0 to (readerObject.FieldCount - 1) do
|
||||
print (readerObject.getName i)
|
||||
|
||||
-- 打印结果:
|
||||
-- readerObject.read()方法将读取器对象中的记录移至下一条记录。
|
||||
while readerObject.read() do (
|
||||
local record = readerObject.item
|
||||
-- 现在打印记录的值。这假设您知道字段名:
|
||||
format "%\t%\t%\n" record["id"] record["name"] record["phone"]
|
||||
|
||||
-- 如果不想使用显式字段名,还有另一种选择:
|
||||
for i = 0 to (readerObject.FieldCount - 1) do
|
||||
format "%\t" record[i]
|
||||
format "\n"
|
||||
)
|
||||
|
||||
-- 关闭读取器对象
|
||||
readerObject.close()
|
||||
|
||||
-- 关闭连接
|
||||
DBConnection.close()
|
||||
)
|
||||
@@ -0,0 +1,30 @@
|
||||
dotNet.loadAssembly "C:\\Program Files (x86)\\MySQL\\MySQL Connector NET 8.2.0\\MySql.Data.dll"
|
||||
|
||||
host = "81.71.134.168" -- The name of the MySQL server
|
||||
database = "admin" -- The name of the database to use
|
||||
user = "admin123" -- The user name to use
|
||||
password = "aiAtB23yajDj2Lpa" -- The password to use
|
||||
connectionString = "Database=" + database + ";Data Source=" + host + ";User Id=" + user + ";Password=" + password
|
||||
DBConnection = dotNetObject "MySql.Data.MySqlClient.MySqlConnection"
|
||||
DBConnection.ConnectionString = connectionString
|
||||
DBConnection.open()
|
||||
-- Print the connection status: 1 - connected; 0 - not connected.
|
||||
print DBConnection.state.value__
|
||||
|
||||
-- Add a new rows
|
||||
cmdObject = DBConnection.CreateCommand()
|
||||
cmdObject.commandText ="INSERT INTO `admin`.`students`(`id`, `name`, `age`, `score`, `birthday`, `insert_time`) VALUES (132, 'name03', 16, 35, '2023-11-23', '2023-12-13 10:50:56')"
|
||||
|
||||
readerObject = cmdObject.ExecuteReader()
|
||||
-- 打印受影响的行数
|
||||
format "添加了 % 行\n" readerObject.RecordsAffected
|
||||
readerObject.close()
|
||||
|
||||
-- 查询数据库:
|
||||
-- 首先创建一个命令对象。该对象用于将查询发送到数据库。
|
||||
cmdObject = DBConnection.CreateCommand()
|
||||
-- 定义要执行的SQL查询:
|
||||
cmdObject.commandText = "SELECT * FROM `admin`.`students`"
|
||||
-- 然后执行命令以创建读取器对象。
|
||||
readerObject = cmdObject.ExecuteReader()
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
try(destroyDialog YeomaiDataBaseConnectionTest)catch()
|
||||
try(dotNet.loadAssembly "C:\\Program Files (x86)\\MySQL\\MySQL Connector NET 8.2.0\\MySql.Data.dll")catch(messagebox("dll载入失败,插件无法使用"))
|
||||
rollout YeomaiDataBaseConnectionTest "无标题" width:837 height:639
|
||||
(
|
||||
label 'lbl1' "服务器" pos:[100,100] width:53 height:13 align:#right
|
||||
label 'lbl2' "数据库名" pos:[100,130] width:53 height:13 align:#right
|
||||
label 'lbl3' "用户名" pos:[100,160] width:23 height:13 align:#right
|
||||
label 'lbl4' "密码" pos:[100,190] width:23 height:13 align:#right
|
||||
editText 'host' "" pos:[170,100] width:241 height:22 align:#left text:"81.71.134.168"
|
||||
editText 'database' "" pos:[170,130] width:241 height:22 align:#left text:"admin"
|
||||
editText 'user' "" pos:[170,160] width:241 height:22 align:#left text:"admin123"
|
||||
editText 'password' "" pos:[170,190] width:241 height:22 align:#left text:"aiAtB23yajDj2Lpa"
|
||||
button 'btn1' "连接数据库" pos:[170,240] width:84 height:23 align:#left
|
||||
button 'btn2' "断开数据库" pos:[280,240] width:84 height:23 align:#left
|
||||
|
||||
on 'btn1' pressed do(
|
||||
connectionString = "Database=" + database.text + ";Data Source=" + host.text + ";User Id=" + user.text + ";Password=" + password.text
|
||||
DBConnection = dotNetObject "MySql.Data.MySqlClient.MySqlConnection"
|
||||
DBConnection.ConnectionString = connectionString
|
||||
DBConnection.open()
|
||||
if DBConnection.state.value__ then messagebox("连接成功!") else messagebox("连接失败!")
|
||||
)
|
||||
|
||||
on 'btn2' pressed do(
|
||||
-- 关闭连接
|
||||
if DBConnection.state.value__ then (DBConnection.close();messagebox("断开连接!"))
|
||||
)
|
||||
|
||||
)
|
||||
createDialog YeomaiDataBaseConnectionTest
|
||||
Reference in New Issue
Block a user