MySQL在cmd和python下的使用示例

這篇文章主要介紹MySQL在cmd和python下的使用示例,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

秀山土家族苗族網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站開發(fā)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)于2013年創(chuàng)立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。

環(huán)境配置1:安裝mysql,環(huán)境變量添加mysql的bin目錄

環(huán)境配置2:python安裝MySQL-Python

請(qǐng)根據(jù)自身操作系統(tǒng)下載安裝,否則會(huì)報(bào)c ++ compile 9.0,import _mysql等錯(cuò)誤

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

一 、cmd命令下的操作:

連接mysql:mysql -u root -p

查看所有數(shù)據(jù)庫:show databases;

創(chuàng)建test數(shù)據(jù)庫:create database test;

刪除數(shù)據(jù)庫:drop database test;

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

查看當(dāng)前數(shù)據(jù)庫下的表:show tables;

創(chuàng)建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;

判斷數(shù)據(jù)是否存在:select * from UserInfo where name like 'elijahxb';

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

查數(shù)據(jù):select * from UserInfo; select id from UserInfo; select username from UserInfo;

改數(shù)據(jù):update UserInfo set username = 'Zus' where id=1; update UserInfo set username='Zus';

刪數(shù)據(jù):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:可以不傳此參數(shù),但此時(shí)value每一條數(shù)據(jù)的字段數(shù)必須與數(shù)據(jù)庫中的字段數(shù)一致。
            傳此參數(shù)時(shí),則表示只穿指定字段的字段值。
    :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)

以上是“MySQL在cmd和python下的使用示例”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

標(biāo)題名稱:MySQL在cmd和python下的使用示例
當(dāng)前鏈接:http://bm7419.com/article18/pccddp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、電子商務(wù)搜索引擎優(yōu)化、網(wǎng)站內(nèi)鏈、網(wǎng)站建設(shè)標(biāo)簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作