python 向mysql插入數據


生成隨機內容用到的方法:

substr是一個字符串函數,從第二個參數1,開始取字符,取到3 + floor(rand() * 75)結束

floor函數代表的是去尾法取整數。

rand()函數代表的是從0到1取一個隨機的小數。

    -- rand() * 75就代表的是:0到75任何一個小數,
    -- 3+floor(rand() * 75)就代表的是:3到77的任意一個數字
concat()函數是一個對多個字符串拼接函數。

sha1是一個加密函數,sha1(rand())對生成的0到1的一個隨機小數進行加密,轉換成字符串的形式。
	-- concat(sha1(rand()), sha1(rand()))就代表的是:兩個0-1生成的小數加密然后進行拼接。

substr(concat(sha1(rand()), sha1(rand())), 1, floor(rand() * 80))就代表的是:從一個隨機生成的一個字符串的第一位開始取,取到(隨機3-77)位結束。

case floor(rand()*10) mod 2 when 1 then 'M' else 'F' end,代表:當余數為1是,就取M,其他的為F

sql語句說明

TMP='''
    set @i :=0;
    create table TMP as select (@i := @i + 1) as id from information_schema.tables limit 10;
'''
nformation_schema.tables表:提供了關於數據庫中的表的信息(包括視圖)。詳細表述了某個表屬於哪個schema,表類型,表引擎,創建時間等信息。是show tables from schemaname的結果取之此表

mysql中變量不用事前申明,在用的時候直接用“@變量名”使用就可以了。set這個是mysql中設置變量的特殊用法,當@i需要在select中使用的時候,必須加:,這樣就創建好了一個表tmp

示例:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time   : 2017/11/22 23:51
# @Author : lijunjiang
# @File   : insert.py



import MySQLdb

# SQL
# 向Student表中從100開始插入1000條隨機數據
Student='''
    set @i := 100;
    insert into Student select @i := @i + 1, substr(concat(sha1(rand()*10) + sha1(rand())),1,3+floor(rand()*75)),case floor(rand()*10) mod 2 when 1 then 'M' else 'F' end, 20+floor(rand() * 5) from TMP a,TMP b,TMP c;
'''

# 向 Cours 表中從10開始插入10條隨機數據
Cours='''
    set @i := 10;
    insert into Course select @i := @i + 1, substr(sha1(rand()),1,2+floor(rand()*10)), 1 + floor(rand()*10) from TMP a;
'''

#向 Score 表中從200開始插入10000條數據
Score='''
    set @i := 200;
    insert into Score select @i := @i + 1, floor(101+rand()*1000), floor(11+rand()*10),floor(1+ rand()*100) from TMP a, TMP b, TMP c, TMP d;
'''

# 向Teacher 表中從1 開始插入100條數據
Teacher='''
    set @i := 0;
    insert into Teacher select @i := @i + 1, substr(sha1(rand()),1,3+floor(rand() * 10));
'''

def connect_mysql():
    db_config = dict(host="11.11.11.11", port=3306, db="python", charset="utf8", user="python", passwd="python")
    try:
        cnx = MySQLdb.connect(**db_config)
    except Exception as err:
        raise err
    return cnx

if __name__ == "__main__":
    # sql = "create table test(id int not null);"
    cnx = connect_mysql()  # 連接mysql
    # cus = cnx.cursor()     # 創建一個游標對象
    try:
        cus = cnx.cursor()
        cus.execute(Student)
        cus.close()

        cus = cnx.cursor()
        cus.execute(Teacher)
        cus.close()

        cus = cnx.cursor()
        cus.execute(Score)
        cus.close()

        cus = cnx.cursor()
        cus.execute(Cours)
        cus.close()

        cnx.commit()
    except Exception as err:
        cnx.rollback()
        raise err
    finally:
        cnx.close()
mysql> select * from Student;
+-------+-----------------------+--------+------+
| StdID | StdName               | Gender | Age  |
+-------+-----------------------+--------+------+
|   101 | 1.79769313486232e+308 | M      |   22 |
|   102 | 97                    | F      |   21 |
|   103 | 4.12e+72              | M      |   24 |
|   104 | 18791937              | F      |   24 |
|   105 | 88                    | F      |   21 |
|   106 | 159612                | M      |   24 |
|   107 | 93                    | F      |   21 |
|   108 | 527                   | F      |   20 |
|   109 | 4                     | M      |   21 |
|   110 | 1.79769313486232e+308 | F      |   23 |

|  1099 | 11515                 | F      |   24 |
|  1100 | 1.79769313486232e+308 | F      |   20 |
+-------+-----------------------+--------+------+
1000 rows in set (0.00 sec)


mysql> select * from Teacher;
+-----+--------------+
| TID | TName        |
+-----+--------------+
|   1 | 8dcd0f6f4c67 |
|   2 | 852c304e     |
|   3 | 23cdcaf356e  |

|  99 | 21bcef63     |
| 100 | 77b76fa88f   |
+-----+--------------+
100 rows in set (0.00 sec)

mysql> select * from Score limit 5;

+-----+-------+-------+-------+
| SID | StdID | CouID | Grade |
+-----+-------+-------+-------+
| 201 |   726 |    18 |     4 |
| 202 |   951 |    12 |    11 |
| 203 |   238 |    14 |    43 |

mysql> select * from Score;
| 10199 |  1010 |    15 |    67 |
| 10200 |   970 |    14 |    23 |
+-------+-------+-------+-------+
10000 rows in set (0.01 sec)


mysql> select * from Course;
+-------+------------+-----+
| CouID | CName      | TID |
+-------+------------+-----+
|    11 | bd3ffa     |   2 |
|    12 | eb8c       |   7 |
|    13 | aab5381    |   1 |
|    14 | 490ed47b02 |   3 |
|    15 | 16328013b  |   1 |
|    16 | a77a91f    |   7 |
|    17 | 226b67b68  |   6 |
|    18 | e36f       |   4 |
|    19 | 7987545374 |   5 |
|    20 | 424b4b1a8  |   7 |
+-------+------------+-----+
10 rows in set (0.00 sec)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM