直接访问mysql,示例如下:

# coding:utf-8
import time
import MySQLdb
# import traceback
# import sys conn = MySQLdb.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='dev', charset="utf8", )
cur = conn.cursor()
# cur.execute("SET NAMES utf8") try:
sql = "create table mytest1("
sql = sql + "id int(11) not null auto_increment,"
sql = sql + "channelcode varchar(100) null,"
sql = sql + "systemcode varchar(100) not null,"
sql = sql + "systemname varchar(100) not null,"
sql = sql + "primary key(id))"
print sql;
cur.execute(sql);
except Exception, e:
print Exception, ":", e try:
cur.execute("insert into mytest1(systemcode,systemname) values('test','测试')")
conn.commit()
print 'insert new mytest1.'
except Exception, e:
print Exception, ":", e
# traceback.print_exc()
# except:
# info=sys.exc_info()
# print info[0],":",info[1] try:
cur.execute("update mytest1 set systemname='测试" + time.strftime('%Y-%m-%d %H:%M:%S') + "' where systemcode='test'");
conn.commit()
except Exception, e:
print Exception, ":", e try:
itsystem = cur.execute("select * from mytest1");
print 'count:' + str(itsystem);
info = cur.fetchmany(itsystem)
for ii in info:
# if print ii,chinese string will print it's unicode with u\####
print str(ii[0]) + ',' + (ii[1] == None and str(None) or ii[1]) + ',' + ii[2] + ',' + ii[3]
except Exception, e:
print Exception, ":", e try:
cur.execute("delete from itsystem where systemcode='test'");
conn.commit() cur.execute("drop table mytest1");
except Exception, e:
print Exception, ":", e
cur.close()
conn.close()

python访问数据库一的更多相关文章

  1. PYTHON访问数据库

    PYTHON DB API(规范)框架 可以一次编写同时访问MySql\ Oracle \SQLServer...不同的数据库服务器:统一接口程序的混乱. 1.连接访问:connection(高速路) ...

  2. python 访问数据库

    commit() 提交rollback() 回滚 cursor用来执行命令的方法:callproc(self, procname, args):用来执行存储过程,接收的参数为存储过程名和参数列表,返回 ...

  3. Python MySQL 数据库

    python DB API python访问数据库的统一接口规范,完成不同数据库的访问 包含的内容: connection cursor exceptions 访问数据库流程: 1.创建connect ...

  4. Python中通过cx_Oracle访问数据库遇到的问题总结

    以下是Python中通过cx_Oracle操作数据库的过程中我所遇到的问题总结,感谢我们测试组的前辈朱勃给予的帮助最终解决了下列两个问题:     1)安装cx_Oracle会遇到的问题:在Windo ...

  5. Python第十三天 django 1.6 导入模板 定义数据模型 访问数据库 GET和POST方法 SimpleCMDB项目 urllib模块 urllib2模块 httplib模块 django和web服务器整合 wsgi模块 gunicorn模块

    Python第十三天   django 1.6   导入模板   定义数据模型   访问数据库   GET和POST方法    SimpleCMDB项目   urllib模块   urllib2模块 ...

  6. python学习(22) 访问数据库

    原文链接:http://www.limerence2017.com/2018/01/11/python22/ 本文介绍python如何使用数据库方面的知识. SQLite SQLite是一种嵌入式数据 ...

  7. Python访问sqlite3数据库取得dictionary的正路!

    [引子] 很多人都知道,Python里是内置了很好用的sqlite3的.但这个库有个缺陷,在执行fetchall()/fetchone()等方法后,得到的是一个tuple.以前吧,做自己的小项目,tu ...

  8. python 访问sql server数据库

    访问数据库 cnxn = pyodbc.connect("Driver={SQL Server};Server=localhost;Database=用户名;uid=sa;pwd=密码&qu ...

  9. Python同步数据库的数据到Neo4J

    写了主要是步骤,如果疑问,请咨询QQ:5988628 Python版本采用2.7.X,默认的2.6.X后期会有问题,建议,一开始就升级Python.然后再安装pip. 访问数据库 sqlalchemy ...

随机推荐

  1. lua2c

    lua2c lua2c is a Lua module and utility to convert Lua 5.1 source code to C API code. http://lua-use ...

  2. JSONP使用笔记

    JSONP JSONP是实现跨域GET请求的一种方法, 原理上利用script标签可以动态加载JS文件, 将不同源站点的JSON数据加载到本网站来,如果给定回调函数,将回调函数名传递到服务器端, 在服 ...

  3. jq 截取字符串

    var str="abcde";截取ab;str=str.substring(0,2);意思是从第一个字符开始截取两位: 一.函数:split() 功能:使用一个指定的分隔符把一个 ...

  4. document.body.clientHeight和 document.documentElement.clientHeight 的区别

    1.javascript中的 document.body.clientHeight 和 document.documentElement.clientHeight 的区别 在往同事负责的页面添加我的功 ...

  5. nmap使用教程

    Nmap是一款开源免费的网络发现(Network Discovery)和安全审计(Security Auditing)工具.软件名字Nmap是Network Mapper的简称.Nmap最初是由Fyo ...

  6. CSS3 中border-image详解

    CSS3 border-image详解.应用及jQuery插件 一.border-image的兼容性 border-image可以说是CSS3中的一员大将,将来一定会大放光彩,其应用潜力真的是非常的惊 ...

  7. Chart 绘制,自带动画效果

    package com.example.canvasdemo; import android.annotation.SuppressLint; import android.content.Conte ...

  8. 迭代器和for-of

    在ES5及之前的JS标准中,我们习惯了用for(var i = 0; i < Array.length;i++){//TODO}或者是for(var index in Array){consol ...

  9. Windows10 如何删掉内置的 skype ?

    打开开始菜单,输入“PowerShell”并回车: 运行“Get-AppxPackage -User username”命令( username 请替换成当前实际用户名),此时会显示所有已安装的应用程 ...

  10. HTML 事件属性

    HTML 事件属性一:定义二:窗口事件 (Window Events)三:表单元素事件 (Form Element Events)四:图像事件 (Image Events) 一:定义 HTML 4 的 ...