濮阳杆衣贸易有限公司

主頁 > 知識庫 > 淺談MySQL在cmd和python下的常用操作

淺談MySQL在cmd和python下的常用操作

熱門標(biāo)簽:聊城智能外呼系統(tǒng)運營商 上海智能外呼系統(tǒng)代理商 電子地圖標(biāo)注電話 ps制作地圖標(biāo)注gif 扎樣申請400電話 成都優(yōu)派外呼系統(tǒng) 沈陽電銷外呼系統(tǒng)原理是什么 寧波企業(yè)外呼系統(tǒng)收費 地圖標(biāo)注人員兼職

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

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

請根據(jù)自身操作系統(tǒng)下載安裝,否則會報c ++ compile 9.0,import _mysql等錯誤

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ù),但此時value每一條數(shù)據(jù)的字段數(shù)必須與數(shù)據(jù)庫中的字段數(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)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 使用Python操作MySQL的一些基本方法
  • 在Python程序中操作MySQL的基本方法
  • Python 操作MySQL詳解及實例
  • Python操作MySQL簡單實現(xiàn)方法
  • Python操作使用MySQL數(shù)據(jù)庫的實例代碼
  • python操作MySQL數(shù)據(jù)庫的方法分享
  • Python中操作MySQL入門實例

標(biāo)簽:林芝 內(nèi)江 宿州 咸寧 汕頭 朔州 三明 AXB

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《淺談MySQL在cmd和python下的常用操作》,本文關(guān)鍵詞  淺談,MySQL,在,cmd,和,python,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《淺談MySQL在cmd和python下的常用操作》相關(guān)的同類信息!
  • 本頁收集關(guān)于淺談MySQL在cmd和python下的常用操作的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    辛集市| 辰溪县| 二连浩特市| 五峰| 咸丰县| 太仆寺旗| 南城县| 资中县| 哈密市| 平阴县| 南溪县| 达拉特旗| 固始县| 囊谦县| 小金县| 闵行区| 尼玛县| 乌什县| 江阴市| 竹溪县| 新密市| 浪卡子县| 大田县| 关岭| 财经| 乌拉特中旗| 靖州| 东至县| 宣汉县| 临高县| 穆棱市| 铜川市| 会昌县| 镇远县| 东源县| 江城| 绍兴市| 长顺县| 冕宁县| 吉安市| 望谟县|