pymysqlモジュール+mysqlライブラリ/テーブルバックアップとリカバリ+トランザクション(ロック)

4360 ワード

目次
  • pymysqlモジュール+mysqlライブラリ/テーブルバックアップ+トランザクション(ロック)
  • pymysqlモジュール
  • クエリーデータ
  • 削除、修正、増加データ
  • sql注入問題
  • pymysqlテーブル/ライブラリバックアップ/リカバリ
  • テーブルのバックアップ/リカバリ
  • ライブラリのバックアップ/リカバリ
  • トランザクション(ロック)

  • pymysqlモジュール+mysqlライブラリ/テーブルバックアップ+トランザクション(ロック)
    pymysqlモジュール
    データの問合せ
    import pymysql
    conn=pymysql.connect(host='127.0.0.1',user='root',password='123',database='homework')    #host      ip  ,user        ,password     ,database    
    
    cur=conn.cursor()   #             cursor=pymysql.cursors.DictCorsor,              
    
    sql='select * from student;' #  ;     
    cur.execute(sql)
    ret1=cur.fetchone()     #          
    print(ret1)
    #(1, ' ', 1, '  ')
    ret2=cur.fetchmany(10)  #          ,  ,           ,            ,      
    print(ret2)
    #((2, ' ', 1, '  '), (3, ' ', 1, '  '), (4, ' ', 1, '  '), (5, ' ', 1, '  '), (6, ' ', 1, '  '), (7, ' ', 2, '  '), (8, ' ', 2, '  '), (9, ' ', 2, '  '), (10, ' ', 2, '  '), (11, ' ', 2, '  '))
    
    ret3=cur.fetchall()		#          
    print(ret3)
    #((12, ' ', 3, '  '), (13, ' ', 3, '  '), (14, ' ', 3, '  '), (15, ' ', 3, '  '), (16, ' ', 3, '  '))
    
    cur.close()
    conn.close()
    
    
    #rowcount:        ,          
    import pymysql
    conn=pymysql.connect(host='127.0.0.1',user='root',password='123',database='homework')
    cur=conn.cursor()
    
    sql='select * from student'
    cur.execute(sql)
    print(cur.rowcount)
    for i in range(cur.rowcount):
        print(cur.fetchone())
    cur.close()
    conn.close()
    16
    (1, ' ', 1, '  ')
    (2, ' ', 1, '  ')
    (3, ' ', 1, '  ')
    (4, ' ', 1, '  ')
    (5, ' ', 1, '  ')
    (6, ' ', 1, '  ')
    (7, ' ', 2, '  ')
    (8, ' ', 2, '  ')
    (9, ' ', 2, '  ')
    (10, ' ', 2, '  ')
    (11, ' ', 2, '  ')
    (12, ' ', 3, '  ')
    (13, ' ', 3, '  ')
    (14, ' ', 3, '  ')
    (15, ' ', 3, '  ')
    (16, ' ', 3, '  ')
    
    

    データの削除、変更、追加
    import pymysql
    conn=pymysql.connect(host='127.0.0.1',user='root',password='123',database='homework')
    cur=conn.cursor()
    
    try:
    	sql='insert into student values(17," ","1","  ");' #mysql     ,  " " ''   
    	cur.execute(sql)
    	conn.commit()   	#          ,     
    except Exception as e:
    	print(e)
    	conn.rollback()		# try       ,         ,sql     
    	
    cur.close()
    conn.close()
    

    sql注入問題
    import pymysql
    name=input("uesrname:")
    pwd=input("password:")
    conn=pymysql.connect(host='127.0.0.1',user='root',password='123',database='users')
    cur=conn.cursor()
    
    sql='select * from userinfo where name=%s and pwd=%s;'  #      "'select * from userinfo where name=%s and pwd=%s'%(name,pwd)",   sql    
    cur.execute(sql,(name,pwd))
    print(cur.fetchone())
    cur.close()
    conn.close()
    

    pymysqlテーブル/ライブラリバックアップ/リカバリ
    テーブルのバックアップ/リカバリ
    #    
    #mysqldump -u   -p   -h     ip    . >       
    C:\Users\81533>mysqldump -uroot -p123 -h127.0.0.1 homework.student>C:\Users\81533\Desktop\mysql_data\student.sql
            
    #    
    mysql>source C:\Users\81533\Desktop\mysql_data\student.sql
    

    ライブラリのバックアップ/リカバリ
  • ライブラリの名前を変更するには、通常、バックアップしてから元のライブラリを削除し、バックアップライブラリのsqlドキュメントでライブラリ名を変更してからライブラリを復元することもできます.
  • #    
    #mysqldump -uroot -p123 --databases   1,  2,....(    --all -databases:      ) >             ;
    C:\Users\81533>mysqldump -uroot -p123 --databases homework>C:\Users\81533\Desktop\mysql_data\homework.sql
            
    #    
    mysql>source homework>C:\Users\81533\Desktop\mysql_data\homework.sql
    

    トランザクション(ロック)
    begin;	#    
    select * from student where id=1 forupdate;	#for update               ,          ,                
    update student set age=18 where id=1;	#       
    commit;	#    ,   ,