python如何连接MySQL数据库
#!/usr/bin/env python
# -*-coding:UTF-8-*-#这一句告诉python用UTF-8编码
#=========================================================================
#
# NAME: Python MySQL test
#
# AUTHOR: benyur
# DATE : 2004-12-28
#
# COMMENT: 这是一个python连接mysql的例子
#
#=========================================================================
"""
***** This is a MySQL test *****
select:
conn=Connection()
conn.select_db('test')
cur=conn.cursor()
cur.execute('select * from user')
cur.scroll(0)
row1=cur.fetchone()
row1[0]
row1[1]
row1[2]
insert:
cur.execute('insert into user (name,passwd) values('benyur','12345')')
cur.insert_id()
update:
cur.execute('update user set passwd='123456' where name='benyur'')
delete:
cur.execute('delete from user where id=2')
**********************************
"""
from MySQLdb import *
def conn():
conn=Connection()
conn.select_db('test')
cur=conn.cursor()
cur.execute('select * from user')
cur.scroll(0)
row1=cur.fetchone()
row1[0]
row1[1]
row1[2]
def usage():
print __doc__
if __name__=='__main__':
usage()
MySQLdb下载地址:http://sourceforge.net/projects/mysql-python/
下载解压缩后放到%Python_HOME%Libsite-packages目录中,python会自动找到此包。
MySQLdb基本上是MySQL C API的Python版,遵循Python Database API Specification v2.0。
相关文档:
插入一条记录:
mysql> insert into table_name (column_name1,column_name2,.) values (value1,value2,..);
更新一条记录:
mysql> update tbl_name
s ......
像PHP和perl一样,MySQL也提供的C语言使用的API. C代码的API是随MySQL一起发布的。 它包含在mysqlclient库中, 可以使C程序来访问数据库。
MySQL源码包中的许多客户端都是用C写的。 如果你正在找使用这些C API的例子, 可以看看客户端的写法。你可以在MySQL源码包的clients目录找到这些例子。
& ......
这个代码是基于python3.0写的,有许多不完善的地方,请自已修改。
# coding: utf-8
from tkinter import *
root = Tk()
root.title("python3.0查询")
#root.minsize(800,600)
#填充无用空间
Label(root).grid(ipady=5)
#11面板
class Frame11:
def __init__(self, root): # ......
修正一下:我在Windows下的实际操作如下
1.关闭正在运行的MySQL。
2.打开DOS窗口,转到mysql\bin目录。
3.输入mysqld解 --skip-grant-tables回车。如果没有出现提示信息,那就对了。
4.再开一个DOS窗口(因为刚才那个DOS窗口已经不能动了),转到mysql\bin目录。
5.输入mysql回车,如果成功,将出现MySQL ......
mysql建立一个表格:
create table hyhtbsc(
id int(4) primary key auto_increment,
username varchar(20),
password varchar(20)
);
查询数据库内容:
<?php
$conn = mysql_connect("localhost","root","pass");
mysql_select_db("mydb",$conn);
$sql = "select * from mytable";
$result = mysql_query($ ......