import pymysql
from sshtunnel import SSHTunnelForwarder
# 连接服务器
server = SSHTunnelForwarder(
('192.168.43.161', 22),
ssh_username='root',
ssh_password='123456',
remote_bind_address=('127.0.0.1', 3306)
)
server.start()
# 创建数据库连接
conn = pymysql.connect(host='127.0.0.1',port=server.local_bind_port,user='root',password='',db='test',
charset='utf8')
# 定义一个数据库游标
cur = conn.cursor()
#编写sql语句
sql1 = 'delete from ecs_order_info'
# 调用游标执行方法执行sql语句
cur.execute(sql1)
# 关闭游标
cur.close()
# 关闭数据库连接
conn.close()
import pymysql
from sshtunnel import SSHTunnelForwarder
# 连接服务器
server = SSHTunnelForwarder(
('192.168.43.161', 22),
ssh_username='root',
ssh_password='123456',
remote_bind_address=('127.0.0.1', 3306)
)
server.start()
# 创建数据库连接
conn = pymysql.connect(host='127.0.0.1',port=server.local_bind_port,user='root',password='',db='test',
charset='utf8')
# 定义一个数据库游标
cur = conn.cursor()
#编写sql语句
#向用户表里添加用户数据
sql1 = "INSERT INTO `test`.`ecs_users` (`email`, `user_name`, `password`,`question`, `answer`, `sex`, `birthday`, `user_money`, `frozen_money`,`pay_points`, `rank_points`, `address_id`, `reg_time`, `last_login`, `last_time`,`last_ip`, `visit_count`, `user_rank`, `is_special`, `ec_salt`, `salt`,`parent_id`, `flag`, `alias`, `msn`, `qq`, `office_phone`, `home_phone`,`mobile_phone`, `is_validated`, `credit_line`, `passwd_question`,`passwd_answer`) VALUES ('mianyi@123.com', '{}','e10adc3949ba59abbe56e057f20f883e', '', '', '0', '0000-00-00', '0.00', '0.00','0', '0', '0', '1721095978', '1721095978', '0000-00-00 00:00:00','192.168.43.200', '1', '0', '0', NULL, '0', '0', '0', '', '123@qq.com', '12344','12344', '12344', '12344', '0', '0.00', 'interest', '吃饭');"
# values = (pymysql.escape_string(data_dict['user_name']))
name_start = 'test'
for i in range(10000):
name = name_start + str(i)
# pymysql.escape_string(name)
cur.execute(sql1.format(name))
#添加1万个用户test0-test9999
conn.commit() #保存
# 关闭游标
cur.close()
# 关闭数据库连接
conn.close()
sql语句可以在查询里做