五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動態(tài)

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

發(fā)布日期:2022-04-12 17:37 | 文章來源:CSDN

環(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)容了,希望能給大家一個參考,也希望大家多多支持本站。

國外服務(wù)器租用

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部