Randomly generate student names, three grades and class data in Python, and then insert them into PostgreSQL.
Module with PSYCOPG2 random
ImportRandomImportPsycopg2fname=['Gold','Zhao','Li','Chen','Xu','Dragon','Wang','High','Zhang','Hou','AI','Money','Sun','Week','Zheng']mname=['Jade','Ming','Ling','Sue','Pescara','Yan','Big','Small','Wind','Rain','Snow','days','Water','Odd','Whale','m','Xiao','ze','Yes','GE','Xuan','Tao','Vibration','Long','Odd']lname=["','Ling',"','Fang','Ming','Red','Country','Fen',"','Cloud','Jung','Hidden',"','Flowers','leaves',"','Yellow','Bright','Kam','Tsuta','Army',"','Printing',"','Kay']#Database Connection Parametersconn = Psycopg2.connect (database="Girl", user="JM", password="123", host="127.0.0.1", port="5432") cur=conn.cursor () forXinchRange (200): #Generate random DataPname=random.choice (fname) +random.choice (mname) +Random.choice (lname) Math=random.randint (40,100) 中文版=random.randint (40,100) Chinese=random.randint (40,100) Pclass=random.randint (1,3) #Insert Data (special note only%s cannot be used, numeric data does not use quotation marksCur.execute ("INSERT into score values (%s,%s,%s,%s,%s,%s)", (X,pname,math,english,chinese,glass)) Conn.commit ()#Submit command, otherwise the database does not perform an insert operationcur.close () conn.close ( )
Random.choice (Sequence): Random selection of an element in a sequence
Randomly generate data in Python and insert it into PostgreSQL