使用Python脚本创建MySQLdb数据库
我在创建数据库和表时遇到问题。数据库需要在Python脚本中创建使用Python脚本创建MySQLdb数据库,python,mysql-python,Python,Mysql Python,我在创建数据库和表时遇到问题。数据库需要在Python脚本中创建 #connect method has 4 parameters: #localhost (where mysql db is located), #database user name, #account password, #database name db1 = MS.connect(host="localhost",user="root",passwd="****",db="test") 返回 _mysql
#connect method has 4 parameters:
#localhost (where mysql db is located),
#database user name,
#account password,
#database name
db1 = MS.connect(host="localhost",user="root",passwd="****",db="test")
返回
_mysql_exceptions.OperationalError: (1049, "Unknown database 'test'")
显然,首先需要创建db1,但是如何创建呢?我尝试在connect()语句之前创建,但出现错误
创建数据库后,如何创建表?
谢谢
汤姆
这是语法,至少第一次是这样。第二次自然返回db已经存在。现在了解如何正确使用drop命令
db = MS.connect(host="localhost",user="root",passwd="****")
db1 = db.cursor()
db1.execute('CREATE DATABASE test1')
所以这在第一次使用时效果很好。第二次至提供警告“db已存在”。如何应对?以下是我认为它应该如何工作的,但事实并非如此。或者它应该是一个if语句,寻找它是否已经存在,不填充
import warnings
warnings.filterwarnings("ignore", "test1")
用于创建数据库:
db1 = MS.connect(host="localhost",user="root",passwd="****")
cursor = db1.cursor()
sql = 'CREATE DATABASE mydata'
cursor.execute(sql)
用于创建表:
sql = '''CREATE TABLE foo (
bar VARCHAR(50) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
'''
cursor.execute(sql)
创建表时有很多选项。如果您不确定正确的SQL应该是什么,可以使用图形工具(如创建表),然后使用SHOW create table
查找创建表所需的SQL:
mysql> show create table foo \G
*************************** 1. row ***************************
Table: foo
Create Table: CREATE TABLE `foo` (
`bar` varchar(50) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
phpmyadmin还可以向您展示它用于执行各种操作的SQL。这是学习一些基本SQL的方便方法
一旦您对此进行了实验,就可以用Python编写SQL了。我认为解决方案要简单得多,请使用“if not”:
我不想太粗鲁,但你试过用谷歌搜索它吗?从connect and go read中删除db=“test”,或者是的,大量的谷歌搜索(RTFM并不粗鲁,但我不会问我是否能找到它)。当我放弃“测试”时,有无错误。问题是如何使用python而不是SQL创建db。第二次自然返回db已经存在。现在了解如何正确使用drop命令。db=MS.connect(host=“localhost”,user=“root”,passwd=“***”)db1=db.cursor()db1.execute('CREATE DATABASE test1')“DROP DATABASE test1”。虽然学习SQL是一件好事,但您可能还想研究对象关系映射器,可以说是最好的映射器。非常感谢。
sql = "CREATE DATABASE IF NOT EXISTS test1"
db1.execute(sql)
import MySQLdb
# Open database connection ( If database is not created don't give dbname)
db = MySQLdb.connect("localhost","yourusername","yourpassword","yourdbname" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
# For creating create db
# Below line is hide your warning
cursor.execute("SET sql_notes = 0; ")
# create db here....
cursor.execute("create database IF NOT EXISTS yourdbname")
# create table
cursor.execute("SET sql_notes = 0; ")
cursor.execute("create table IF NOT EXISTS test (email varchar(70),pwd varchar(20));")
cursor.execute("SET sql_notes = 1; ")
#insert data
cursor.execute("insert into test (email,pwd) values('test@gmail.com','test')")
# Commit your changes in the database
db.commit()
# disconnect from server
db.close()
#OUTPUT
mysql> select * from test;
+-----------------+--------+
| email | pwd |
+-----------------+--------+
| test@gmail.com | test |
+-----------------+--------+