学习Python脚本和SQL数据库使用,为了保持多设备代码的版本同步,折腾把脚本代码保存到SQL数据表中。
方便一起备份,用到方便更新,也可以再写个批量插入文件和更新文件脚本,网页端读取使用。
可以使用图形工具软件,连数据库建立表
表 vps2022.text 结构
CREATE TABLE IF NOT EXISTS `text` ( `name` text, `text` text ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
获取sql数据表 text 中所有脚本文件的代码
getsh.py
import mysql.connector
conn = mysql.connector.connect(
host="127.0.0.1",
user="user",
passwd="passwd" )
c = conn.cursor()
c.execute("USE vps2022")
sql = 'SELECT * FROM text ORDER BY name'
c.execute(sql)
text = c.fetchall()
def makefile(file, str):
f = open(file, 'w')
f.write(str); f.close()
for i in range(len(text)):
file=text[i][0]
file_str = text[i][3]
makefile(file, file_str)
print(file, "\t保存完成!")
conn.close()
选择下载一个文件 getfile.py
import mysql.connector
conn = mysql.connector.connect(
host="127.0.0.1",
user="user", passwd="pswd",
database="vps2022", buffered = True )
c = conn.cursor()
sql = 'SELECT name FROM text'
c.execute(sql)
names = c.fetchall()
# 显示所有文件名,以供下载
for i in range(len(names)):
print(names[i][0], end=' ')
print('\n:: 选择一个文件下载,输入文件名: ', end='')
name = input().strip()
sql = 'SELECT * FROM text WHERE name="' + name +'"'
c.execute(sql)
text = c.fetchall()
def makefile(file, str):
f = open(file, 'w')
f.write(str); f.close()
for i in range(len(text)):
file=text[i][0]
file_str = text[i][5]
makefile(file, file_str)
print(file, "\t保存完成!")
conn.close()
使用 addfile.py
可以批量上传文件到数据库中
import sys, glob
import mysql.connector
conn = mysql.connector.connect(
host="127.0.0.1",
user="user", passwd="passwd",
database="vps2022", buffered = True )
c = conn.cursor()
files = []
for f in sys.argv[1:]:
files = files + glob.glob(f)
def readfile(file):
f = open(file, 'r')
str = f.read(); f.close()
return str
for file in files:
file_str = readfile(file)
row =(file, file_str)
c.execute("DELETE FROM text WHERE name=%s ", (file,) )
c.execute('INSERT INTO text VALUES (%s,%s)', row)
print("FontName: " + file + " ....OK")
conn.commit()
conn.close()
相关: 使用php获取文件名和文件
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = mysqli_connect("18.18.18.18", "root", "实际密码", "vps2022","53306");
$result = mysqli_query($mysqli, "SELECT name FROM text");
// $result = mysqli_query($mysqli, "SELECT * FROM text WHERE name = 'addfile.py'");
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC); // MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH
foreach ($rows as $row) {
printf("%s\n", $row["name"]);
// printf("%s (%s)\n", $row["name"], $row["text"]);
}
var_dump($rows);
/** MYSQLI_ASSOC
array(11) {
[0]=>
array(1) {
["name"]=>
string(10) "addfile.py"
** MYSQLI_NUM
array(11) {
[0]=>
array(1) {
[0]=>
string(10) "addfile.py"
*/
- MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH 可选参数是一个常量,指示应从当前行数据生成什么类型的数组。