ホームページ > バックエンド開発 > Python チュートリアル > Python の SQL データベースの追加、削除、変更、クエリ操作を簡単にカプセル化する方法

Python の SQL データベースの追加、削除、変更、クエリ操作を簡単にカプセル化する方法

高洛峰
リリース: 2017-03-04 16:36:29
オリジナル
1843 人が閲覧しました

この記事の例では、Python を使用してデータベースの追加、削除、変更、クエリを簡単にカプセル化する方法を共有します。具体的な内容は次のとおりです

1.insert

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

import mysql.connector

import os

import codecs

#设置数据库用户名和密码

user='root';#用户名

pwd='root';#密码

host='localhost';#ip地址

db='mysql';#所要操作数据库名字

charset='UTF-8'

cnx = mysql.connector.connect(user=user,password=pwd, host=host, database=db)

#设置游标

cursor = cnx.cursor(dictionary=True)

#插入数据

#print(insert('gelixi_help_type',{'type_name':'\'sddfdsfs\'','type_sort':'283'}))

def insert(table_name,insert_dict):

  param='';

  value='';

  if(isinstance(insert_dict,dict)):

    for key in insert_dict.keys():

      param=param+key+","

      value=value+insert_dict[key]+','

    param=param[:-1]

    value=value[:-1]

  sql="insert into %s (%s) values(%s)"%(table_name,param,value)

  cursor.execute(sql)

  id=cursor.lastrowid

  cnx.commit()

  return id

ログイン後にコピー

2.削除

1

2

3

4

5

6

7

8

9

10

def delete(table_name,where=''):

  if(where!=''):

    str='where'

    for key_value in where.keys():

      value=where[key_value]

      str=str+' '+key_value+'='+value+' '+'and'

    where=str[:-3]

    sql="delete from %s %s"%(table_name,where)

    cursor.execute(sql)

    cnx.commit()

ログイン後にコピー

3.select

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

#取得数据库信息

# print(select({'table':'gelixi_help_type','where':{'help_show': '1'}},'type_name,type_id'))

def select(param,fields='*'):

  table=param['table']

  if('where' in param):

    thewhere=param['where']

    if(isinstance (thewhere,dict)):

      keys=thewhere.keys()

      str='where';

      for key_value in keys:

        value=thewhere[key_value]

        str=str+' '+key_value+'='+value+' '+'and'

      where=str[:-3]

  else:

    where=''

  sql="select %s from %s %s"%(fields,table,where)

  cursor.execute(sql)

  result=cursor.fetchall()

  return result

ログイン後にコピー

4.showtable、showcolumns

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

#显示建表语句

#table string 表名

#return string 建表语句

def showCreateTable(table):

  sql='show create table %s'%(table)

  cursor.execute(sql)

  result=cursor.fetchall()[0]

  return result['Create Table']

#print(showCreateTable('gelixi_admin'))

#显示表结构语句

def showColumns(table):

  sql='show columns from %s '%(table)

  print(sql)

  cursor.execute(sql)

  result=cursor.fetchall()

  dict1={}

  for info in result:

    dict1[info['Field']]=info

  return dict1

ログイン後にコピー

上記は、Python SQL データベースの追加、削除、修正と動作の確認が皆さんの学習に役立つことを願っています。

Python の SQL データベースの追加、削除、変更、クエリ操作の簡単なカプセル化方法に関するその他の関連記事については、PHP 中国語 Web サイトに注目してください。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート