找回密码
 加入
搜索
查看: 227|回复: 0

需要连续完成多条MYSQL语句的读写,能有什么更好的方法吗?

[复制链接]
发表于 2023-8-17 17:31:53 | 显示全部楼层 |阅读模式
写个客户端,有时候需要连续对数据库操作,按通常的做法,一次只能一个语句,效率低下。
尝试过一次代入多条SQL语句(句末分号结尾),一是代码变得难以辨认,再者好像也没能调试成功。

如:$query = "UPDATE db_A SET field6 = IFNULL("& $value6 &",0) WHERE ID='"& $user_ID &"';UPDATE db_B SET field7 = IFNULL('" & $value7 & "',0) WHERE ID='"& $user_ID &"';"
writedb($query)

所以想求助各位大佬,有无思路:提高效率,减少服务器压力?
$query = "INSERT INTO db_A(field1,field2,field3,field4,field5) VALUES('" &value1 & "','" & $value2 & "','" & $value3 & "','" & $value4 & "','" & $value5 & "');"
writedb($query)

$query = "SELECT * FROM db_B WHERE ID ='" & $user_ID & "';"
readdb_B($query)

$query = "SELECT * FROM db_A WHERE ID ='" & $user_ID & "';"
readdb_A($query)

$query = "UPDATE db_A SET field6 = IFNULL("& $value6 &",0) WHERE ID='"& $user_ID &"';"
writedb($query)

$query = "UPDATE db_B SET field7 = IFNULL('" & $value7 & "',0) WHERE ID='"& $user_ID &"';"
writedb($query)

#cs
【关于写库】考虑到各数据库结构不同,字段数量不同,从数组中获取的字段值也不同
所以自定义函数用了多个,如:Func readdb_A($query),Func readdb_B($query),Func readdb_C($query)等等,
感觉好繁琐,每次执行一个操作,都需要对数据库进行开闭操作
就像一把土铳,每打一枪,需要重新擦枪管,装火药。。。 哪位大神给个机关枪的设计思路呢?
【关于写库】可通用一个Func writedb($query)
#ce

Func readdb_A($query)
; ……(从略,主题结构与B相同,但获取字段不同)
EndFunc ;==>readdb_A($query)

Func readdb_B($query)
_MySQL_InitLibrary()
$MysqlConn = _MySQL_Init()
$connected = _MySQL_Real_Connect($MysqlConn, $serverIP, $userID, $userPD, $DBname,3306)
If $connected = 0 Then Exit MsgBox(16, "连接数据库服务器失败", "抱歉,连接数据库服务器失败...程序退出,请联系管理员!",30)
_mysql_set_character_set($MysqlConn, "GBK")
_MySQL_Real_Query($MysqlConn, $query,StringLen(_StringToHex($query)) /2);——此处支持中文功能采用“sunless”网友的方案,在此表示感谢!
$res = _MySQL_Store_Result($MysqlConn)
$fields = _MySQL_Num_Fields($res)
$rows = _MySQL_Num_Rows($res)
If $rows = 0 Then
MsgBox(16+262144, "服务器消息", "连接数据库服务器失败", 30)
Exit
Else
Dim $array[$rows][$fields]
For $k = 1 To $rows
$mysqlrow = _MySQL_Fetch_Row($res, $fields)
$lenthsStruct = _MySQL_Fetch_Lengths($res)
For $i = 1 To $fields
$length = DllStructGetData($lenthsStruct, 1, $i)
$fieldPtr = DllStructGetData($mysqlrow, 1, $i)
$data = DllStructGetData(DllStructCreate("char[" & $length & "]", $fieldPtr), 1)
$array[$k - 1][$i - 1] = $data
Next
Next
;显示搜索结果
;_ArrayDisplay($array,"监测点",-1,0,"","","行号|数据库ID|显示文字|字符大小|字符颜色|用户类型")
$db_B_ID = $array[0][0]
$field1 = $array[0][2];用户类型
$field4 = $array[0][5];显示文字的颜色
EndIf
;关闭数据库
_MySQL_Data_Seek($res, 0)
_MySQL_Free_Result($res)
_MySQL_Close($MysqlConn)
_MySQL_EndLibrary()
EndFunc ;==>readdb_B($query)

;写库操作到是可以通用,不过也是一次1颗子弹
Func writedb($query)
_MySQL_InitLibrary()
$MysqlConn = _MySQL_Init()
$connected = _MySQL_Real_Connect($MysqlConn, $serverIP, $userID, $userPD, $DBname,3306);(,服务器名,ID,密码,数据库名)
If $connected = 0 Then
MsgBox(16, "连接数据库服务器失败", _MySQL_Error($MysqlConn),6)
Exit
EndIf
_mysql_set_character_set($MysqlConn,"gbk")
_MySQL_Real_Query($MysqlConn, $query,StringLen(_StringToHex($query)) /2);——此处支持中文功能采用“sunless”网友的方案,在此表示感谢!
$res = _MySQL_Store_Result($MysqlConn)
;关闭数据库
_MySQL_Data_Seek($res, 0)
_MySQL_Free_Result($res)
_MySQL_Close($MysqlConn)
_MySQL_EndLibrary()
EndFunc ;==>writedb

您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-4-30 23:55 , Processed in 0.071886 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表