拼接index

import MySQLdb
import sys

db = MySQLdb.connect(host="127.0.0.1",    # your host, usually localhost
                     user="root",         # your username
                     passwd="111111",  # your password
                     db="test",
                     port=3312)        # name of the data base

# you must create a Cursor object. It will let
#  you execute all the queries you need
cur = db.cursor()

# Use all the SQL you like
try:
    cur.execute("show create table memory_test")

except MySQLdb.Error as e:
    print e
    sys.exit(1)


# print all the first cell of all the rows

for row in cur.fetchall():
    ddl_statement = row[1]

#print ddl_statement
line_sum = len(ddl_statement.splitlines())
if 3 >= line_sum:
    sys.exit(1)

line_index = 0
new_create_table_statement = ""
for ddl_statement_line in ddl_statement.splitlines():
    #print(ddl_statement_line)
    if line_index == line_sum - 2:
        new_create_table_statement += (ddl_statement_line + ",\n")
        new_create_table_statement += ("index idx_name(`name`)" + "\n")
        #print ddl_statement_line
        #print("index idx_name(`name`)")
    else:
        new_create_table_statement += (ddl_statement_line + ",\n")
    line_index += 1
print new_create_table_statement
db.close()

 

posted @ 2018-09-27 10:35  友哥  阅读(153)  评论(0编辑  收藏  举报