MySQL數(shù)據(jù)庫技巧基礎(chǔ)操作:增、刪、改、查,具體sql語句如下:
import console;
import mysql.client;
console.log("正在連接數(shù)據(jù)庫服務(wù)器...");
var mysqlDb,err = mysql.client(
server = "localhost"; //數(shù)據(jù)庫服務(wù)器,可省略默認(rèn)為localhost
uid = "root";//用戶名,可省略默認(rèn)為root
pwd = "用戶密碼";
);
if(!mysqlDb){
console.log("連接數(shù)據(jù)庫失敗",err)
return;
}
//如果數(shù)據(jù)庫不存在創(chuàng)建數(shù)據(jù)庫,并自動(dòng)選擇數(shù)據(jù)庫
mysqlDb.createDb("testAAutoMySqlClient")
sql = /*
CREATE TABLE `library` (
`name` varchar(100) NOT NULL,
`auditing` tinyint(1) DEFAULT '0',
`bytes` BLOB,
PRIMARY KEY (`name`),
UNIQUE KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
*/
mysqlDb.exec(sql);//執(zhí)行SQL語句
var ret,err = mysqlDb.listTables()
for tbl in ret.each(){
console.log("發(fā)現(xiàn)數(shù)據(jù)表:",tbl);
}
//增 - 使用命名參數(shù)
mysqlDb.exec("REPLACE INTO `library` values('名字1',@num,@str)
,('名字2',@num,@str)
,('名字3',@num,@str)
", {
num = 123;
str = "測(cè)試:'這是字符串!'命名參數(shù)可以自動(dòng)處理字符串轉(zhuǎn)義";
} )
//刪 - 使用format格式化參數(shù)
mysqlDb.exec("DELETE FROM `%s` where name='%s';","library",'名字3')
//改
mysqlDb.exec("UPDATE `library` SET auditing = 111 WHERE name = '名字2';" );
//查詢數(shù)據(jù)并返回記錄集,limit指定返回記錄集索引
var result = mysqlDb.query("select * from `library` limit 0,10");
for name,auditing,bytes in result.each(){
io.print( name,auditing,bytes ); //逐行輸出所有記錄
}
//移除數(shù)據(jù)庫
mysqlDb.dropDb("testAAutoMySqlClient");
更多信息請(qǐng)查看IT技術(shù)專欄