Randomly generate data in Python and insert it into PostgreSQL

Source: Internet
Author: User
Tags ming postgresql

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.