VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > python入门教程 >
  • 原生python操作mysql

 
# -*- coding: utf-8 -*-
import subprocess

class MySQL(object):
    def __init__(self):
        self.sql = None
        self.re  = None
        self.out = None
        print("程序初始化.....")
        self.connection()

    def connection(self):
        print("请输入数据库连接信息.")
        user = input("登录账号: ")
        pwd  = input("登录密码: ")
        cmd = ['mysql', f'-u{user}', f'-p{pwd}']
        self.re = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True)
        print("连接成功!")
        self.query()

    def query(self):
        self.sql = input("mysql> ")
        self.re.stdin.write(self.sql)
        self.out = self.re.communicate()[0]
        print(self.out)

if __name__ == '__main__':
    MySQL()


相关教程