Getting started with Python read it over the internet
The key is not to use
Slowly practice using a small online meeting project
python connects to the database,Python uses version 3.6, and since the MYSQLDB module does not support python3.x, python3.x need to install the Pymysql module if you want to connect to MySQL.
The Pymysql module can be installed via PIP. Pip Install Pymysql
Installing Phpstudy
Phpstudy is a program integration package for a PHP debugging environment. The package integrates the latest apache+php+mysql+phpmyadmin+zendoptimizer, one-time installation, no need to be configured to use, is a very convenient and useful PHP debugging environment • This program includes not only the PHP debugging environment, but also the development tools, Development manuals, etc. • Learn PHP In a nutshell with just one package. For beginners who learn PHP, the environment is difficult to configure under Windows, which is a chore for the veteran. So whether you're a novice or a veteran, the package is a good choice. Install it and run Phpstudy later.
to see if the connection database was successful,python code
Import Pymysql.cursors
# connect to database connect = Pymysql. Connect (host= ' localhost ', #localhost地址是本地 port=3306, #端口 user= ' root ', #用户名 passwd= ' root ', #密码 db= ' trade ', #数据库名称 CharSet = ' UTF8 ' #编码)
Fill in the above method:
Analyze the fields that your project might use:
Meeting: Meeting ID, name, meeting content, creation time, creator, owner
Issue: Issue ID, meeting ID, issue content, problem execution, creator, owner
Meeting Room: Conference room name, location, whether occupied
Add fields to the table in the MySQL database, and field names are usually expressed in English
Python code
Objective to define a class meeting to enable Python to delete and modify data table
# get cursors cursor = Connect.cursor () class meeting (): Def __init__ (Self,meetingid,name,meetingcontent,creator,belongto, CreationTime):#类的属性, the field name of the corresponding database meeting tableSelf. Meetingid=meetingid Self.creator=creator Self. Meetingcontent=meetingcontent Self.name=name Self. Belongto=belongto self. Creationtime=creationtime def insert_data (self):#定义一个插入数据的函数, the parameter passed in is self itself# Insert Data sql = "INSERT into meeting (Meetingid,name,meetingcontent,creator,belongto,creationtime) VALUES ('%s ', '%s ', '%s ', '%s ', '%s ', '%s ') ' data = (Self . Meetingid,Self.name,self. Meetingcontent,self.creator,self. Belongto,self. creationtime) cursor.execute (sql% data)#合并Connect.commit () print (' Successful insert ', Cursor.rowcount, ' data ') def delete_data (self,ID):#此处传参, the first argument must be self, the second parameter ID is a parameter that matches the condition, and the name is random, where the instance is the ' 003 ' that is passed in when the function is called# delete Data sql = "Delete from meeting WHERE Meetingid = '%s '" data = (ID) cursor.execute (sql% data) Connect.commit () print (' Successful delete ', Cursor.rowcount, ' data ') def Modify_da Ta (self,tname,tid): # Modify DataTry: #try的功能是用来处理异常, here is Data= (self.name,self) Because of the wrong parameter in the first data. Meetingid), the result has been error 1054, so use try to print the exception, later understand that the value of Self.name is the name value of the incoming T ' GZ meeting 'sql = "UPDATE meeting SET name= '%s ' WHERE meetingid = '%s '" data = (Tname, tid) templesql=sql%data Cursor.execute (sql% data) Connect.commit () print (' Successfully modified ', Cursor.rowcount, ' data ')except Exception as E:print (e) #打印异常代码def query_data (self,creator): # query data sql = "SELECT name from meeting WHERE Creator = '%s '" data = (c reator) cursor.execute (sql% data) for row in Cursor.fetchall (): Print ("name:%s\t"% row) Print (' co-search Find ', cursor.rowcount, ' data ') t=meeting (' 004 ', ' GZ conference ', ' Balara Barrala ' La la la, ' Lucys ', ' adminisratosr ', ' 2018-01-02 ') T.insert_data () t.modify_data (' 666 ', ' 003 ') t.query_data (' Lucy ') T.delete_data ('003‘)# Close Connection Cursor.close () Connect.close ()
Knowledge Points:
1. In the beginning, I was confused about self in the class. What is the meaning of the property, and how to use it, all the arguments in the method are self.**,
def query_data (self,self.creator): # query data sql = "SELECT name from meeting WHERE Creator = '%s '" data = (self.creator)
And then I realized
(self.) Meetingid, Self.name,self. Meetingcontent,self.creator,self. Belongto,self. CreationTime) corresponds to meeting (' 004 ', ' GZ conference ', ' Balara Barrala-la-la-la-la-cheerleaders ', ' Lucys ', ' adminisratosr ', ' 2018-01-02 '), which are formal and actual parameters
2.
Try:#Try function is used to handle exceptions
Except Exception as E:
Print (e)#打印异常代码
0 Basic Learning python, first day