python连接数据库并插入数据
1.Python创建数据库
import MySQLdb
try:
conn = MySQLdb.connect(
host="127.0.0.1",
port=3306,
user="root",
passwd="",
# db="juntest",#也可以在这一步显示数据库名
charset="utf8")
cur = conn.cursor()
cur.execute('CREATE DATABASE IF NOT EXISTS juntestDBnew DEFAULT CHARSET utf8 COLLATE utf8_general_ci;') #没有这个表,就创建
conn.close()
print u"创建数据库juntestDBnew成功! "
except MySQLdb.Error, e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
创建成功,查看
2.创建数据表
#coding=utf-8
import MySQLdb
try:
conn = MySQLdb.connect(
host="127.0.0.1",
port=3306,
user="root",
passwd="",
# db="juntest",另外写一种建表的方法
charset="utf8")
conn.select_db('juntestDBnew')#选择一个数据库
cur = conn.cursor()
cur.execute("drop table if exists emp_info2;")
cur.execute("drop table if exists salary2;")
cur.execute('''create table emp_info2(
id int not null auto_increment,
name varchar(30) not null,
sex char(4) default null,
dept varchar(10),
mobile varchar(11) not null unique,
birthday date default "0000-00-00",
primary key(id)
)engine=innodb character set utf8 comment 'employer info';
''')
cur.execute('''create table salary2(
id int not null auto_increment,
emp_id int not null,
salary int not null,
primary key(id)
)engine=innodb character set utf8 comment 'employer salary info';
''')
cur.close()
conn.close()
print u"创建数据表成功! " except MySQLdb.Error, e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
查看是否成功
3.进行数据的插入
#coding=utf-8
import MySQLdb
import random try:
conn = MySQLdb.connect(
host="127.0.0.1",
port=3306,
user="root",
passwd="",
db="juntestDBnew",#另外写一种建表的方法
charset="utf8"
)
cur=conn.cursor()
num=random.randint(10,300)
sql="insert into emp_info2 values(%s,%s,%s,%s,%s,%s)"#这种写法自增字段和隐藏字段都要写出来
#cur.execute(sql, (4, 'jun1', 'm', 'jun', '12345', '2017-4-30'));
for i in range(1,10):
cur.execute('delete from emp_info2 where id='+str(i));
#第一种插入
cur.execute(sql, (i, 'jun'+str(i), 'm', 'jun', ''+str(random.randint(1,1000)), '2017-4-'+str(random.randint(1,30))));
print u"插入第(%s)条数据成功"%i
cur.close()
conn.commit()
conn.close() except MySQLdb.Error, e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
python连接数据库并插入数据的更多相关文章
- python 定义一个插入数据(可以插入到每个表中)通用的方法
前提置要:想要写一个方法,这个方法是插入数据到数据表的方法,只需要提供表名称,字段名称,还有插入的值,只要调用这个方法就可以自动帮助你插入数据 以下是不断实践优化出来 原本的插入数据库中的代码应该是这 ...
- go : 连接数据库并插入数据
package main import ( "database/sql" "fmt" "log" "net/http" ...
- python 向mysql插入数据
生成随机内容用到的方法: substr是一个字符串函数,从第二个参数1,开始取字符,取到3 + floor(rand() * 75)结束 floor函数代表的是去尾法取整数. rand()函数代表的是 ...
- Java使用JDBC连接数据库逐条插入数据、批量插入数据、以及通过SQL语句批量导入数据的效率对比
测试用的示例java代码: package com.zifeiy.test.normal; import java.io.File; import java.io.FileOutputStream; ...
- python向数据库插入数据时出现乱码解决方案
中文字符串前面加u 如: title =u"你好" contents = "m" ids="13" cur.execute("IN ...
- 集群Redis使用 Python pipline大批量插入数据
class myRedis(object): def __init__(self,redis_type=None,**args): if redis_type == " ...
- 单节点Redis使用 Python pipline大批量插入数据
方法一: import redis import time filename = 'redis_resulit.txt' def openPool(): pool = redis.Connec ...
- Python中MySQL插入数据
sql = 'INSERT INTO course(class_name, credit, properties, teacher_name, college_given, classroom) ' ...
- 解决Python往MySQL插入中文时报错的问题
今天遇到一个问题,用Python往MySQL插入数据时,若数据中包含中文会报类似下面的错误: ERROR 1366: Incorrect string value: '\xE4\xB8\xAD\xE5 ...
随机推荐
- Angular2-编写一个简易的组件
Angular2组件可以这么理解:编写一个类,然后在类的上面用组件装饰器装饰一下,这个类就成组件了. 所以编写组件分两步:1)编写类:2)编写装饰器 1)编写类: export class Simpl ...
- c#之泛型详解(Generic)
这篇文章主要来讲讲c#中的泛型,因为泛型在c#中有很重要的位置,对于写出高可读性,高性能的代码有着关键的作用. 一.什么是泛型? 泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个非 ...
- 使用 OLEDB 及 SqlBulkCopy 将多个不在同一文件夹下的 ACCESS mdb 数据文件导入MSSQL
注:转载请标明文章原始出处及作者信息http://www.cnblogs.com/z-huifei/p/7380388.html 前言 OLE DB 是微软的战略性的通向不同的数据源的低级应用程序接口 ...
- UML 简介笔记
1. UML 是什么? UML 统一建模语言是一组图形表示法,可以帮助描述和设计软件系统,特别是使用面向对象 OO 风格建造的软件系统. 2. 使用 UML 的方式 UML 有 3 种使用模式:草稿, ...
- vue指令示例合集
vue所有指令练习合集.这是个html文件,用chrome打开可查看结果. <!DOCTYPE html> <html lang="en" xmlns:v-on= ...
- Postman如何调试
在用Postman接口测试过程当中,肯定少不了调试,下面记录一下Postman如何通过控制台输出进行调试: 一.打开控制台(View-Show Postman Console) 二.预置测试数据(测试 ...
- bootstrap学习笔记细化(按钮)
button:btn 圆角灰色按钮 button:btn btn-default 圆角灰色边框按钮 button:btn btn-success 绿色 button:btn btn-primary 蓝 ...
- WDCP服务器升级之后伪静态缓存文件.htaccess读取失效
当购买或者升级服务器之后,thinkphp3框架的默认缓存文件.htaccess读取失效,解决方法如下: 1.我的网站是thinkphp3 服务器环境是N+A模式 2.将网站根目录的.htaccess ...
- webstorm git 怎么断开版本控制 webstorm git for windows 禁止 自动运行
也是无语啊,今天装了下最新版本的webstorm , 发现特别卡,老动不动就卡死, 看了下进程, 牛X 啊, git for windows 一直蹭蹭蹭的疯狂增长,一开始的一点到后来的庞然大物. ...
- 如何在Ubuntu上安装gcc-6.3
装显卡驱动推荐 gcc 6.3 版本,其实linux上多个版本的gcc是可以共存的,需要的的时候切换就好,参加之前的博客 https://www.cnblogs.com/jins-note/p/951 ...