MySQLデータベース_MySQLとPythonの相互作用_Python操作MySQLの手順と追加、削除、変更

MySQLデータベース

MySQLとPythonの相互作用

Python操作MySQLステップ

pysql01

インポートモジュール

  • pyファイルにpymysqlモジュールを導入します
from pymysql import *

接続オブジェクト

  • データベースとの接続を確立するために使用されます
  • オブジェクトを作成します:connect()メソッドを呼び出します
conn=connect(参数列表)
  • パラメータhost:ローカルマシンが「localhost」の場合、接続されたmysqlホスト
  • パラメータport:接続されたmysqlホストのポート。デフォルトは3306です。
  • パラメータデータベース:データベースの名前
  • パラメータuser:接続のユーザー名
  • パラメータパスワード:接続のパスワード
  • パラメータ文字セット:通信に使用されるエンコード方法、utf8をお勧めします

オブジェクトメソッド

  • close()接続を閉じます
  • commit()送信
  • cursor()は、SQLステートメントを実行して結果を取得するために使用されるCursorオブジェクトを返します。

カーソルオブジェクト

  • SQLステートメントの実行に使用され、最も頻繁に使用されるステートメントは、select、insert、update、deleteです。
  • Cursorオブジェクトを取得します。Connectionオブジェクトのcursor()メソッドを呼び出します。
cs1=conn.cursor()

オブジェクトメソッド

  • close()close
  • execute(operation [、parameters])はステートメントを実行し、影響を受ける行数を返します。主に挿入、更新、削除ステートメントの実行に使用され、create、alter、dropなどのステートメントも実行できます。
  • fetchone()がクエリステートメントを実行すると、クエリ結果セットの最初の行データを取得し、タプルを返します。
  • fetchall()がクエリを実行すると、結果セットのすべての行が取得され、1つの行がタプルを形成してから、これらの要素をタプルにアセンブルして戻ります。

オブジェクトのプロパティ

  • rowcountは読み取り専用属性であり、最新のexecute()実行後に影響を受けた行数を示します。
  • connectionは、現在の接続オブジェクトを取得します

PythonはMySQLを操作して、「追加、削除、変更、およびチェック」を実行します

追加と削除

from pymysql import *

def main():
    # 创建Connection连接
    conn = connect(host='localhost',port=3306,database='jing_dong',user='root',password='此处填写密码',charset='utf8')
    # 获得Cursor对象
    cs1 = conn.cursor()
    # 执行insert语句,并返回受影响的行数:添加一条数据
    # 增加
    count = cs1.execute('insert into goods_cates(name) values("硬盘")')
    #打印受影响的行数
    print(count)

    count = cs1.execute('insert into goods_cates(name) values("光盘")')
    print(count)

    # # 更新
    # count = cs1.execute('update goods_cates set name="机械硬盘" where name="硬盘"')
    # # 删除
    # count = cs1.execute('delete from goods_cates where id=6')

    # 提交之前的操作,如果之前已经之执行过多次的execute,那么就都进行提交
    conn.commit()

    # 关闭Cursor对象
    cs1.close()
    # 关闭Connection对象
    conn.close()

if __name__ == '__main__':
    main()

データの行をクエリする

from pymysql import *

def main():
    # 创建Connection连接
    conn = connect(host='localhost',port=3306,user='root',password='此处填写码',database='jing_dong',charset='utf8')
    # 获得Cursor对象
    cs1 = conn.cursor()
    # 执行select语句,并返回受影响的行数:查询一条数据
    count = cs1.execute('select id,name from goods where id>=4')
    # 打印受影响的行数
    print("查询到%d条数据:" % count)

    for i in range(count):
        # 获取查询的结果
        result = cs1.fetchone()
        # 打印查询的结果
        print(result)
        # 获取查询的结果

    # 关闭Cursor对象
    cs1.close()
    conn.close()

if __name__ == '__main__':
    main()

データの複数の行をクエリする

from pymysql import *

def main():
    # 创建Connection连接
    conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
    # 获得Cursor对象
    cs1 = conn.cursor()
    # 执行select语句,并返回受影响的行数:查询一条数据
    count = cs1.execute('select id,name from goods where id>=4')
    # 打印受影响的行数
    print("查询到%d条数据:" % count)

    # for i in range(count):
    #     # 获取查询的结果
    #     result = cs1.fetchone()
    #     # 打印查询的结果
    #     print(result)
    #     # 获取查询的结果

    result = cs1.fetchall()
    print(result)

    # 关闭Cursor对象
    cs1.close()
    conn.close()

if __name__ == '__main__':
    main()

おすすめ

転載: blog.csdn.net/weixin_42250835/article/details/90694183