python如何创建mysql数据库_用python创建MySQL数据库

博客展示了如何执行带参数的Python文件,如./file.py server adress arg2 arg3 arg4 。还给出了Python使用MySQLdb模块连接MySQL数据库的代码示例,包括执行SQL查询获取数据库版本,最后提供了使用内置模块argparse或optparse处理参数的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I want to execute a file with parameters like this : ./file.py server adress arg2 arg3 arg4

file1.py :

import MySQLdb

# Open database connection

db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )

# prepare a cursor object using cursor() method

cursor = db.cursor()

# execute SQL query using execute() method.

cursor.execute("SELECT VERSION()")

# Fetch a single row using fetchone() method.

data = cursor.fetchone()

print "Database version : %s " % data

# disconnect from server

db.close()

解决方案

Use the built-in module argparse. Or optparse if you're stuck on Python 2.6 or earlier.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值