python mysql数据库、多线程

python mysql数据库、多线程,第1张

1.mysql数据、数据表删除:
(a).删除数据:

import mysql.connector
mydb = (host = 'localhost' ,user = 'root', password = '123456',basetable = 'my_summary')
mycurcor = mydb.curcor
sql = 'DELETE FROM sites WHERE name = 'Taobao''
mycursor.execute(sql)
#第二种删除方法:
sql = 'DELETE FROM sites WHERE name = %s'
na = ('Taobao')
mycursor.execute(sql,na)
mydb.commit()
print(mycursor,rowcount,'条已删除.')
输出:
	1 条已删除.

(b).删除数据表:

  import mysql.connector
    mydb = (host = 'localhost' ,user = 'root', password = '123456',basetable = 'my_summary')
    mycurcor = mydb.curcor
    sql = 'DROP TABLE IF EXISTS sites' #删除sites表
    mycursor.execute(sql)
    mydb.commit()
    print(mycursor,rowcount,'条已删除.')
    输出:
	1 条已删除.

2.mysql数据更改:

import mysql.connector
    mydb = (host = 'localhost' ,user = 'root', password = '123456',basetable = 'my_summary')
    mycurcor = mydb.curcor
    sql = 'UPDATE sites SET name = %s WHERE name  = %s'
    val = ('Google','google')
    mycursor.execute(sql,val)
    mydb.commit()
    print(mycursor.rowcount, " 记录已被修改.")
    输出:
           1 记录已经被修改.

3.Thread类方法:
run():表示线程活动的方法;
start():启动线程活动;
join(time):等待线程中止;
isAlive():是否返回线程活动;
getName():返回线程名;
setName():设置线程名.
4.多线程(_thread模块):_thread.start_new_thread(function,args,*kwargs)函数

import _thread , time
def fun(name,delay):
    count = 0
    while count < 5:
        time.sleep(delay)
        count += 1
        print(('%s:%s') %(name,time.ctime(time.time())))
try:
    _thread.start_new_thread(fun,('Thread - 1',2,))
    _thread.start_new_thread(fun,('Thread - 2',4,))
except:
    print('Eroor:无法启动.')
    
while 1:
     pass    
输出:
Thread - 1:Mon Apr  4 17:51:56 2022
Thread - 2:Mon Apr  4 17:51:58 2022
Thread - 1:Mon Apr  4 17:51:58 2022
Thread - 1:Mon Apr  4 17:52:00 2022
Thread - 2:Mon Apr  4 17:52:02 2022
Thread - 1:Mon Apr  4 17:52:02 2022
Thread - 1:Mon Apr  4 17:52:04 2022
Thread - 2:Mon Apr  4 17:52:06 2022
Thread - 2:Mon Apr  4 17:52:10 2022
Thread - 2:Mon Apr  4 17:52:14 2022

5.多线程(threading模块):threading.Tread()函数

import threading , time
def fun(name,delay):
    count = 0
    while count < 5:
        time.sleep(delay)
        count += 1
        print(('%s:%s') %(name,time.ctime(time.time())))
obj1 = fun('thread-1',1)
obj2 = fun('thread-2',2)
try:
    threading.Thread(obj1).start()
    threading.Thread(obj2).start()
except:
    print('Eroor:无法启动.')
    
输出:
thread-1:Mon Apr  4 17:43:49 2022
thread-1:Mon Apr  4 17:43:50 2022
thread-1:Mon Apr  4 17:43:51 2022
thread-1:Mon Apr  4 17:43:52 2022
thread-1:Mon Apr  4 17:43:53 2022
thread-2:Mon Apr  4 17:43:55 2022
thread-2:Mon Apr  4 17:43:57 2022
thread-2:Mon Apr  4 17:43:59 2022
thread-2:Mon Apr  4 17:44:01 2022
thread-2:Mon Apr  4 17:44:03 2022

Process finished with exit code 0

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/571656.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)

保存