python的mysql小代码
我因为懒,就想写个批量insert数据的小代码 这里是代码
# _*_ encoding:utf-8 _*_
import os
import MySQLdb
import numpy as np dbname = 'spj'
db = MySQLdb.connect(host="localhost", port=3306, user="root", passwd="", db=dbname,charset="utf8") # 进行插入操作时需要声明编码类型
cursor = db.cursor() def dp_insert(file_name,putin):
file = open(file_name, "r")
for line in file.readlines():#插入
arr = line.split()
sql = putin+' values '+'('+"'"+arr[0]+"'"+','+"'"+arr[1]+"'"+','+"'"+arr[2]+"'"+','+"'"+arr[3]+"'"')'+';'
try:
cursor.execute(sql)
db.commit()
except:
print("Error")
file.close() def select(putin):
sql = putin
try:
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
print(row)
db.commit()
except:
print("Error: unable to fecth data") def describe(putin):
sql = putin
try:
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
print(row)
db.commit()
except:
print("Error: unable to fecth data") def delete_line(infile,outfile):
infopen = open(infile,'r')
outfopen = open(outfile,'w')
lines = infopen.readlines()
for line in lines:
if line.split():
outfopen.writelines(line)
else:
outfopen.writelines("")
infopen.close()
outfopen.close()
os.remove(infile)
os.rename(outfile,infile) def main():
file_name = 'p.txt'
print("please input sql:")
putin = input()
delete_line(file_name, 'new.txt') # test pass
if putin.startswith('insert'): # +','+"'"+arr[2]+"'"
print("数据的path")
file_name = input()
dp_insert(file_name, putin)
elif putin.startswith('select'):
select(putin)
elif putin.startswith('describe'):
describe(putin)
else:
db.close()
os._exit() if __name__ == '__main__':
main()
print("next?")
if input()=='Y':
main()
else:
db.close()
os._exit()
db.close() #insert 模板
#insert into p(pno, sname, color, weight)
(insert语句:insert into j(jno,jname,city))

这里是一些文件处理的代码,之前放在代码里,看着难受,放在这里
# 读取path目录下的文件名,返回文件名list列表
def readFileName(path):
lists = []
for root, dirs, files in os.walk(path):
for file in files:
lists.append(os.path.join(root, file))
return lists # 删除路径为filepath的文件
def delFile(filepath):
os.remove(filepath)
"ok"
''' '''
#转化为数组
for line in open("p.txt","r"):
arr =line.split()
print(arr) #转化为列表
file = open("p.txt", "r")
list_arr = file.readlines()
for i in range(len(list_arr)):
list_arr[i] = list_arr[i].strip()
a = np.array(list_arr)
print(a)
file.close()
'''
#sql=''+''+''+'' 加号连接 sql=‘%s%s%s’ % ('','','')
python的mysql小代码的更多相关文章
- Python操作Mysql实例代码教程在线版(查询手册)_python
实例1.取得MYSQL的版本 在windows环境下安装mysql模块用于python开发 MySQL-python Windows下EXE安装文件下载 复制代码 代码如下: # -*- coding ...
- Python操作Mysql实例代码教程在线版(查询手册)
本文介绍了Python操作MYSQL.执行SQL语句.获取结果集.遍历结果集.取得某个字段.获取表字段名.将图片插入数据库.执行事务等各种代码实例和详细介绍,代码居多,是一桌丰盛唯美的代码大餐 实 ...
- Python之字符串小代码解析
本篇只是拿一段代码来对python中的字符串的一些使用做解释,来让大家更加了解python Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25: ...
- Python装饰器小代码
# coding=utf-8import timedef outer(fun): def inner(): start = time.time() fun() runtime = time.time( ...
- 解决Python往MySQL插入中文时报错的问题
今天遇到一个问题,用Python往MySQL插入数据时,若数据中包含中文会报类似下面的错误: ERROR 1366: Incorrect string value: '\xE4\xB8\xAD\xE5 ...
- 利用Python操作MySQL数据库
前言 在工作中,我们需要经常对数据库进行操作,比如 Oracle.MySQL.SQL Sever 等,今天我们就学习如何利用Python来操作 MySQL 数据库. 本人环境:Python 3.7.0 ...
- 使用Python编程语言连接MySQL数据库代码
使用Python编程语言连接MySQL数据库代码,跟大家分享一下: 前几天我用python操作了mysql的数据库,发现非常的有趣,而且python操作mysql的方法非常的简单和快速,所以我把代码分 ...
- Python小代码_2_格式化输出
Python小代码_2_格式化输出 name = input("name:") age = input("age:") job = input("jo ...
- Python小代码_1_九九乘法表
Python小代码_1_九九乘法表 max_num = 9 row = 1 while row <= max_num: col = 1 while col <= row: print(st ...
随机推荐
- HTML5 Canvas绚丽的小球详解
实例说明: 实例使用HTML5+CSS+JavaScript实现小球的运动效果 掌握Canvas的基本用法 技术要点: 从需求出发 分析Demo要实现的功能 擅于使用HTML5 Canvas 参考手册 ...
- arcgis api for js 4.4 绘图小工具
目前在4.x API中还未有官方的绘图工具,而实际项目中又需要这样的绘图工具,所以自己写了一个. 奉上代码. 使用方法:1.将引入的API模块传入构造函数 var drawTools = new D ...
- 获取百度地图POI数据二(准备搜索关键词)
上篇讲到 想要获取尽可能多的POI数据 需要准备尽可能多的搜索关键字 那么这些关键字如何得来呢? 本人使用的方法是通过一些网站来获取这些关键词 http://poi.mapbar.com ...
- Qunar入职前自学笔记
Q1. 什么是html,html发展历程 hyperText markup language 超文本标记语言 HTML是用于描述网页文档的标记语言.现在我们常常习惯于用数字来描述HTML的版本(如:H ...
- 阻止事件冒泡传播stopPropagation() 阻止自身默认行为preventdefault()
stopPropagation 简单理解:子元素的点击事件 不会去触发父元素的点击事件 preventdefault 简单理解:当点击提交按钮时(submit) 阻止对表 ...
- asp.net core 实战项目(一)——ef core的使用
数据库设计 数据结构图如下: 此次实例比较简单,暂时只设计到上述3张表 SMUser:用于存储用户信息. Role:用于存储角色信息. SMUser_Role:用建立用户和角色关系的一直关联表. ...
- web前端技术学习
$.ajax() ajax数据请求方式,交互,跨域等相关问题 一.请求方式 1.$.ajax() $.ajax({ type:"get",//请求方式“get”和“post” ur ...
- springboot整合mybatics PLUS
首先添加maven依赖: <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactI ...
- python文本处理,format方法--转子网上 crifan
原文出处:https://www.crifan.com/python_string_format_fill_with_chars_and_set_alignment/ [问题] 想要获得这样的效果: ...
- servlet之小demo(四)
描述: 1.在web.xml文件中设置两个web应用的初始化参数,user和password 2.定义一个login.html,里面定义两个请求字段:user,password,发送请求到loginS ...