MySQLdb:
https://pypi.python.org/pypi/MySQL-python/1.2.4
import MySQLdb
1.Download Connector/Python:mysql-connector-python-2.1.3-py2.7-win32
Mysql:mysql-installer-community-5.5.48.0
Python:python-2.7.10
2.安装好Python,Mysql
Connector/Python: http://dev.mysql.com/doc/connector-python/en/connector-python-installation.html
Python3不再支持mysqldb,
取而代之的是:import pymysql
Python MySQL 官方包:mysql-connector-python
3.测试代码
Mysql:查看所有用户 select * from mysql.user;
show databases ; ----------查看全部数据库
use test; --使用数据库
# -*- coding: utf8 -*- import mysql.connector conn=mysql.connector.connect(
host='127.0.0.1',
port=3306,
user='root',
passwd='1qaz@WSX',
db='test',
) cursor = conn.cursor()
cursor.execute ("SELECT VERSION()")
row = cursor.fetchone () print "server version:", row[0] cursor.close()
conn.close()
4.管理数据库
DbVisualizer 8.0.8管理
CREATE TABLE 表名 (字段名 VARCHAR(20), 字段名 CHAR(1));
create table pet(
name varchar(20), #名字
owner varchar(20),#主人
species varchar(20), #种类
sex char(1), #性别
birth date, #出生日期
death date,#死亡日期)
show tables;
select * from tb;
要建立外键关系,首先要保证用来建立外键关系的列具有唯一性,即具有 UNIQUE 约束
通常是某表的主键作为另外一个表的外键
mysql版本和驱动版本不匹配
An error occurred while executing the database request for:
MySQL
5.5.47-log
MySQL-AB JDBC Driver
mysql-connector-java-5.1.16 ( Revision: ${bzr.revision-id} )
Short message:
An error occurred while performing the operation:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
The command that caused the problem:
ALTER TABLE testdjango.student ADD CONSTRAINT student_fk1 FOREIGN KEY (ScoreID) REFERENCES testdjango.pet ()
Long Message:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
Details:
Type: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException
Error Code: 1064
SQL State: 42000
System Information:
Product: DbVisualizer Personal 8.0.8
Build: #1745 (2012/02/14 10:52)
Java VM: Java HotSpot(TM) Client VM
Java Version: 1.6.0_20
Java Vendor: Sun Microsystems Inc.
OS Name: Windows 7
OS Arch: x86
OS Version: 6.1
下载更新驱动:
mysql-connector-java-5.1.38.zip
最后更新到dbvis jar包到相应的目录下就行
DbVisualizer-8.0.8\jdbc\mysql
失败原因:
http://www.cnblogs.com/edisonfeng/archive/2014/06/05/3766243.html
Msql终端添加:
alter table student add constraint score_student_fk1 foreign key (ScoreID) references score(ScoreID)
alter table student add constraint score_student_fk1 foreign key (ScoreID) references score(ScoreID)