python3.6安装mysqllab

一、安装

  1. 经过查资料python3.x不再支持mysqldb。其替代模块是PyMySQL
  2. 用管理员打开cmd

    pip3 install PyMySQL
  3. 链接数据库创建一个数据库

    
    mysql -uroot -proot
    create database python;
    use python;
    
  4. 创建一个表

    CREATE TABLE `users` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `email` varchar(255) COLLATE utf8_bin NOT NULL,
        `password` varchar(255) COLLATE utf8_bin NOT NULL,
        PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
    AUTO_INCREMENT=1 ;
  5. 写一个python文件

    import pymysql
    import pymysql.cursors
    
    
    # 链接数据库
    
    connection = pymysql.connect(host='localhost',
                                 user='root',
                                 password='root',
                                 db='python',
                                 charset='utf8',
                                 cursorclass=pymysql.cursors.DictCursor)
    
    try:
        with connection.cursor() as cursor:
            # 添加一条新的记录
            sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
            cursor.execute(sql, ('[email protected]', 'very-secret'))
    
        # 提交结果
        connection.commit()
    
        with connection.cursor() as cursor:
            # 读取结果
            sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
            cursor.execute(sql, ('[email protected]',))
            result = cursor.fetchone()
            print(result)
    finally:
        connection.close()

  6. 看到以上结果就代表安装成功了;

二 、尾巴

1.如有错误欢迎大家指出,我会及时更正,有什么不懂也可以留言提问,互相交流吗。
2.也许大家觉得这没什么,但是我会认真对待,把它当成我的笔记、心得、这样才能提升自己。

欢迎访问我的csdn博客

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章