<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      MySQL在cmd與python下的常用設置解析

      [摘要]本文主要為大家帶來一篇淺談MySQL在cmd和python下的常用操作。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望能幫助到大家。環境配置1:安裝mysql,環...
      本文主要為大家帶來一篇淺談MySQL在cmd和python下的常用操作。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望能幫助到大家。

      環境配置1:安裝mysql,環境變量添加mysql的bin目錄

      環境配置2:python安裝MySQL-Python

      請根據自身操作系統下載安裝,否則會報c ++ compile 9.0,import _mysql等錯誤

      windows10 64位操作系統可到 http://www.lfd.uci.edu/~gohlke/pythonlibs/ 下載安裝MySQL-Python包,至于whl和tar.gz在windows和Linux下的安裝方法可查看我的上一篇文章

      一 、cmd命令下的操作:

      連接mysql:mysql -u root -p

      查看所有數據庫:show databases;

      創建test數據庫:create database test;

      刪除數據庫:drop database test;

      使用(切換至)test數據庫:use test;

      查看當前數據庫下的表:show tables;

      創建UserInfo表:create table UserInfo(id int(5) NOT NULL auto_increment,username varchar(10),password varchar(20) NOT NULL,PRIMARY KEY(id));

      刪除表:drop table UserInfo;

      判斷數據是否存在:select * from UserInfo where name like 'elijahxb';

      增數據:insert into UserInfo(username,password) value('eljiahxb','123456');

      查數據:select * from UserInfo; select id from UserInfo; select username from UserInfo;

      改數據:update UserInfo set username = 'Zus' where id=1; update UserInfo set username='Zus';

      刪數據:delete from UserInfo; delete from UserInfo where id=1;

      斷開連接:quit

      二、python下的操作:

      # -*- coding: utf-8 -*-
      #!/usr/bin/env python
      
      # @Time  : 2017/6/4 18:11
      # @Author : Elijah
      # @Site  : 
      # @File  : sql_helper.py
      # @Software: PyCharm Community Edition
      import MySQLdb
      
      class MySqlHelper(object):
        def __init__(self,**args):
          self.ip = args.get("IP")
          self.user = args.get("User")
          self.password = args.get("Password")
          self.tablename = args.get("Table")
          self.port = 3306
          self.conn = self.conn = MySQLdb.Connect(host=self.ip,user=self.user,passwd=self.password,port=self.port,connect_timeout=5,autocommit=True)
          self.cursor = self.conn.cursor()
      
        def Close(self):
          self.cursor.close()
          self.conn.close()
        def execute(self,sqlcmd):
          return self.cursor.execute(sqlcmd)
        def SetDatabase(self,database):
          return self.cursor.execute("use %s;"%database)
        def GetDatabasesCount(self):
          return self.cursor.execute("show databases;")
        def GetTablesCount(self):
          return self.cursor.execute("show tables;")
        def GetFetchone(self, table = None):
          if not table:
            table = self.tablename
          self.cursor.execute("select * from %s;"%table)
          return self.cursor.fetchone()
        def GetFetchmany(self,table=None,size=0):
          if not table:
            table = self.tablename
          count = self.cursor.execute("select * from %s;"%table)
          return self.cursor.fetchmany(size)
        def GetFetchall(self,table=None):
          '''
          :param table: 列表
          :return:
          '''
          if not table:
            table = self.tablename
          self.cursor.execute("select * from %s;"%table)
          return self.cursor.fetchall()
        def SetInsertdata(self,table=None,keyinfo=None,value=None):
          """
          :param table:
          :param keyinfo:可以不傳此參數,但此時value每一條數據的字段數必須與數據庫中的字段數一致。
                  傳此參數時,則表示只穿指定字段的字段值。
          :param value:類型必須為只有一組信息的元組,或者包含多條信息的元組組成的列表
          :return:
          """
          if not table:
            table = self.tablename
          slist = []
          if type(value)==tuple:
            valuelen = value
            execmany = False
          else:
            valuelen = value[0]
            execmany = True
          for each in range(len(valuelen)):
            slist.append("%s")
          valuecenter = ",".join(slist)
          if not keyinfo:
            sqlcmd = "insert into %s values(%s);"%(table,valuecenter)
          else:
            sqlcmd = "insert into %s%s values(%s);" % (table,keyinfo,valuecenter)
          print(sqlcmd)
          print(value)
          if execmany:
            return self.cursor.executemany(sqlcmd,value)
          else:
            return self.cursor.execute(sqlcmd, value)

      相關推薦:

      如何利用CMD連接本機mysql數據庫

      如何登錄mysql以及cmd如何連接mysql數據庫?

      php 中執行cmd命令的方法

      以上就是MySQL在cmd和python下的常用操作解析的詳細內容,更多請關注php中文網其它相關文章!


      學習教程快速掌握從入門到精通的SQL知識。




      日韩精品一区二区三区高清