• ubuntu16.04 卸载及安装MySQL


    以MySQL- 5.7.18为例:

    sudo apt-get autoremove --purge mysql-server-5.7
    #sudo apt-get remove mysql-server # 没用到,已经没有mysql-server
    #sudo apt-get autoremove mysql-server # 没用到,已经没有mysql-server
    sudo apt-get remove mysql-common
    sudo rm -rf /etc/mysql/ /var/lib/mysql #很重要
    #清理残留数据
    dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P
    sudo apt autoremove
    sudo apt autoreclean

    安装MySQL:

    sudo apt-get install mysql-server   密码
    sudo apt-get install mysql-client
    sudo apt-get install libmysqlclient-dev

     启动、关闭服务和查看运行状态
    sudo service mysql start
    sudo service mysql stop
    sudo service mysql status


    安装pymysql:

    pip3 install pymysql


    方式一:

    使用pymysql.connect方法来连接数据库

    1
    2
    3
    4
    5
    import pymysql
     
    conn = pymysql.connect(host=None, user=None, password="",
                     database=None, port=0, unix_socket=None,
                     charset=''......)
      • host:表示连接的数据库的地址
      • user:表示连接使用的用户
      • password:表示用户对应的密码
      • database:表示连接哪个库
      • port:表示数据库的端口
      • unix_socket:表示使用socket连接时,socket文件的路径
      • charset:表示连接使用的字符集 
      • read_default_file:读取mysql的配置文件中的配置进行连接

    方式二:

    import pymysql
     
    def connect_mysql():
     
        db_config = {
            'host':'127.0.0.1',
            'port':3306,
            'user':'root',
            'password':'abc.123',
            'charset':'utf8'
        }
     
        conn = pymysql.connect(**db_config)
     
        return conn
     
    # 注意:端口不能加引号,因为port接受的数据类型为整型
    # 注意:charset的字符集不是utf-8,是utf8


    连接

    调用connect函数,将创建一个数据库连接并得到一个Connection对象,Connection对象定义了很多的方法和异常。

    • begin:开始事务
    • commit:提交事务
    • rollback:回滚事务
    • cursor:返回一个Cursor对象
    • autocommit:设置事务是否自动提交
    • set_character_set:设置字符集编码
    • get_server_info:获取数据库版本信息

    在实际的编程过程中,一般不会直接调用begin、commit和rollback函数,而是通过上下文管理器实现事务的提交与回滚操作。



  • 相关阅读:
    应用上架前如何知道自己应用的下载地址?
    Multi-line NSAttributedString with truncated text
    Adding AirDrop File Sharing Feature to Your iOS Apps
    Add sharing to your app via UIActivityViewController
    [原]iOS自带社会化分享框架——Social.framework
    xcode 制作静态库.a文件 详解
    Fiddler怎么对IPhone手机的数据进行抓包分析
    Mac上的抓包工具Charles
    30、准确计算CoreText高度的方法
    keil MDK中如何生成*.bin格式的文件
  • 原文地址:https://www.cnblogs.com/qq_841161825/p/8807867.html
Copyright © 2020-2023  润新知