Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to explain the operation method of Python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to explain how to operate Python. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Mysql is an excellent open source database, and it is widely used now, so it is necessary to briefly introduce the method of operating mysql database with python. Python operating the database requires the installation of a third-party module.

Because the database module of Python has a special specification of database module, in fact, no matter which database is used, the method is more or less the same. Here is an example of Python operation code:

#-*-encoding: gb2312-*-import os, sys, string import MySQLdb # Connect to the database try: conn = MySQLdb.connect (host='localhost',user='root',passwd='xxxx',db='test1') except Exception, e: print e sys.exit () # get the cursor object to operate cursor = conn.cursor () # create the table sql = "create table if not exists test1 (name varchar Age int (4)) "cursor.execute (sql) # insert data sql =" insert into test1 (name, age) values ('% slots,% d) "% (" zhaowei ", 23) try: cursor.execute (sql) except Exception, e: print e sql =" insert into test1 (name, age) values ('% slots,% d) "% (" Zhang San ", 21) try: cursor.execute (sql) except Exception E: print e # insert multiple sql = "insert into test1 (name, age) values (% s,% s)" val = (("Li Si", 24), ("Wang Wu", 25), ("Hong Liu", 26)) try: cursor.executemany (sql, val) except Exception E: print e # query data sql = "select * from test1" cursor.execute (sql) alldata = cursor.fetchall () # if any data is returned For loop output, alldata has a two-dimensional list if alldata: for rec in alldata: print rec [0], rec [1] cursor.close () conn.close () after reading the above, do you have any further understanding of how to explain how to operate Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report