使用python 对mysql 进行 增、删、改、查1

时间:2022-09-21 22:43:39

python +PYmysql 模块:


程序框架:

        --引入模块: import pymysql

           -- 获取连接:db = pymysql.connect(host="localhost",

                                                                       username="root"

                                                                        password='12345'

                                                                        db_name='database'

                                                                           charset = 'utf8')

        -- 获取游标:cursor = db.cursor()

        -- 写入内容 strmysql = "        "

        -- 执行内容:try:

                                    cursor.execute(  strmysql  )

                                     db.commit(   )  注意: 只有在增加时需要事务提交

                                    cursor.fetcall( )\fetchone( )\fetchmany( ) 注意;获取的是一                                                                                                                个地址 

                            except:

                                       rollback(  )

        -- 关闭连接: db.close(  )