首页
学习
活动
专区
工具
TVP
发布

sktj

专栏作者
1542
文章
1901212
阅读量
34
订阅数
Mysql 索引(学习笔记十二)
MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度。
用户5760343
2022-05-23
4070
python 操作DB
import os from random import randrange as rand COLSIZ = 10 FIELDS = ('login', 'userid', 'projid') RDBMSs = {'s': 'sqlite', 'm': 'mysql', 'g': 'gadfly'} DBNAME = 'test' DBUSER = 'root' DB_EXC = None NAMELEN = 16 tformat = lambda s: str(s).title().ljust(COLSIZ) cformat = lambda s: s.upper().ljust(COLSIZ) def setup(): return RDBMSs[raw_input(''' Choose a database system: (M)ySQL (G)adfly (S)QLite Enter choice: ''').strip().lower()[0]] def connect(db): global DB_EXC dbDir = '%s_%s' % (db, DBNAME) if db == 'sqlite': try: import sqlite3 except ImportError: try: from pysqlite2 import dbapi2 as sqlite3 except ImportError: return None DB_EXC = sqlite3 if not os.path.isdir(dbDir): os.mkdir(dbDir) cxn = sqlite3.connect(os.path.join(dbDir, DBNAME)) elif db == 'mysql': try: import MySQLdb import _mysql_exceptions as DB_EXC except ImportError: return None try: cxn = MySQLdb.connect(db=DBNAME) except DB_EXC.OperationalError: try: cxn = MySQLdb.connect(user=DBUSER) cxn.query('CREATE DATABASE %s' % DBNAME) cxn.commit() cxn.close() cxn = MySQLdb.connect(db=DBNAME) except DB_EXC.OperationalError: return None elif db == 'gadfly': try: from gadfly import gadfly DB_EXC = gadfly except ImportError: return None try: cxn = gadfly(DBNAME, dbDir) except IOError: cxn = gadfly() if not os.path.isdir(dbDir): os.mkdir(dbDir) cxn.startup(DBNAME, dbDir) else: return None return cxn def create(cur): try: cur.execute(''' CREATE TABLE users ( login VARCHAR(%d), userid INTEGER, projid INTEGER) ''' % NAMELEN) except DB_EXC.OperationalError: drop(cur) create(cur) drop = lambda cur: cur.execute('DROP TABLE users') NAMES = ( ('aaron', 8312), ('angela', 7603), ('dave', 7306), ('davina',7902), ('elliot', 7911), ('ernie', 7410), ('jess', 7912), ('jim', 7512), ('larry', 7311), ('leslie', 7808), ('melissa', 8602), ('pat', 7711), ('serena', 7003), ('stan', 7607), ('faye', 6812), ('amy', 7209), ('mona', 7404), ('jennifer', 7608), ) def randName(): pick = set(NAMES) while pi
用户5760343
2022-05-14
5370
python QA站点
git链接地址:https://github.com/huangantai/sihelper
用户5760343
2019-11-21
8860
没有更多了
社区活动
RAG七天入门训练营
鹅厂大牛手把手带你上手实战,赢鹅厂证书、公仔好礼!
Python精品学习库
代码在线跑,知识轻松学
博客搬家 | 分享价值百万资源包
自行/邀约他人一键搬运博客,速成社区影响力并领取好礼
技术创作特训营·精选知识专栏
往期视频·千货材料·成员作品 最新动态
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档