[{"data":1,"prerenderedAt":3270},["ShallowReactive",2],{"post-2025-07-12-python-python":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"tags":11,"body":14,"_type":3264,"_id":3265,"_source":3266,"_file":3267,"_stem":3268,"_extension":3269},"\u002Fblog\u002F2025-07-12-python-python","blog",false,"","Python-操作数据库","Python数据库操作：sqlite3\u002Fsqlalchemy连接、CRUD操作、事务处理与连接池。","2025-07-12",[12,13],"Python","基础",{"type":15,"children":16,"toc":3248},"root",[17,22,29,50,56,64,71,99,199,204,270,287,293,311,390,400,415,434,439,447,452,477,482,662,668,673,678,683,692,697,848,861,869,885,891,897,902,918,946,977,982,990,1016,1195,1200,1208,1214,1219,1233,1238,1271,1276,1284,1312,1465,1485,1493,1498,1521,1541,1549,1554,1577,1595,1603,1608,1638,1643,1657,1662,1670,1678,1684,1689,1703,1708,1716,1744,1897,1901,1909,1915,1920,1934,1939,1947,1959,2107,2112,2119,2125,2131,2136,2141,2170,2175,2183,2188,2193,2201,2214,2392,2426,2434,2439,2452,2460,2465,2536,2541,2555,2560,2817,2822,2830,2836,2841,2849,2876,2890,2915,2925,3188,3193,3229,3234,3242],{"type":18,"tag":19,"props":20,"children":21},"element","hr",{},[],{"type":18,"tag":23,"props":24,"children":26},"h2",{"id":25},"数据库编程接口",[27],{"type":28,"value":25},"text",{"type":18,"tag":30,"props":31,"children":32},"p",{},[33,35,41,43,48],{"type":28,"value":34},"在项目开发中，数据库应用必不可少。虽然数据库的种类有很多，如SQLite、MySQL、Oracle等，但是它们的功能基本都是一样的，为了对数据库进行统一的操作，大多数语言都提供了简单的、标准化的数据库接口(API)。在Python Database API 2.0规范中，定义了Python数据库API接口的各个部分，如",{"type":18,"tag":36,"props":37,"children":38},"strong",{},[39],{"type":28,"value":40},"模块接口、连接对象、游标对象、类型对象和构造器、DB API的可选扩展以及可选的错误处理机制",{"type":28,"value":42},"等。下面重点介绍一下",{"type":18,"tag":36,"props":44,"children":45},{},[46],{"type":28,"value":47},"数据库API接口中的连接对象和游标对象",{"type":28,"value":49},"。",{"type":18,"tag":51,"props":52,"children":54},"h3",{"id":53},"连接对象",[55],{"type":28,"value":53},{"type":18,"tag":30,"props":57,"children":58},{},[59],{"type":18,"tag":36,"props":60,"children":61},{},[62],{"type":28,"value":63},"数据库连接对象（Connection Object）主要提供获取数据库游标对象和提交、回滚事务的方法，以及关闭数据库连接。",{"type":18,"tag":65,"props":66,"children":68},"h5",{"id":67},"_1获取连接对象",[69],{"type":28,"value":70},"1）获取连接对象",{"type":18,"tag":30,"props":72,"children":73},{},[74,76,83,85,90,92,97],{"type":28,"value":75},"如何获取连接对象呢？这就需要使用",{"type":18,"tag":77,"props":78,"children":80},"code",{"className":79},[],[81],{"type":28,"value":82},"connect()",{"type":28,"value":84},"函数。该函数有多个参数，具体使用哪个参数，取决于使用的数据库类型。例如，需要访问Oracle数据库和MySQL数据库，则必须同时下载Oracle和MySQL数据库模块。这些模块在获取连接对象时，都需要使用",{"type":18,"tag":77,"props":86,"children":88},{"className":87},[],[89],{"type":28,"value":82},{"type":28,"value":91},"函数。",{"type":18,"tag":77,"props":93,"children":95},{"className":94},[],[96],{"type":28,"value":82},{"type":28,"value":98},"函数常用的参数及说明如表所示。",{"type":18,"tag":100,"props":101,"children":102},"table",{},[103,129],{"type":18,"tag":104,"props":105,"children":106},"thead",{},[107],{"type":18,"tag":108,"props":109,"children":110},"tr",{},[111,121],{"type":18,"tag":112,"props":113,"children":115},"th",{"align":114},"center",[116],{"type":18,"tag":36,"props":117,"children":118},{},[119],{"type":28,"value":120},"参    数",{"type":18,"tag":112,"props":122,"children":123},{"align":114},[124],{"type":18,"tag":36,"props":125,"children":126},{},[127],{"type":28,"value":128},"说    明",{"type":18,"tag":130,"props":131,"children":132},"tbody",{},[133,147,160,173,186],{"type":18,"tag":108,"props":134,"children":135},{},[136,142],{"type":18,"tag":137,"props":138,"children":139},"td",{"align":114},[140],{"type":28,"value":141},"dsn",{"type":18,"tag":137,"props":143,"children":144},{"align":114},[145],{"type":28,"value":146},"数据源名称，给出该参数表示数据库依赖",{"type":18,"tag":108,"props":148,"children":149},{},[150,155],{"type":18,"tag":137,"props":151,"children":152},{"align":114},[153],{"type":28,"value":154},"user",{"type":18,"tag":137,"props":156,"children":157},{"align":114},[158],{"type":28,"value":159},"用户名",{"type":18,"tag":108,"props":161,"children":162},{},[163,168],{"type":18,"tag":137,"props":164,"children":165},{"align":114},[166],{"type":28,"value":167},"password",{"type":18,"tag":137,"props":169,"children":170},{"align":114},[171],{"type":28,"value":172},"用户密码",{"type":18,"tag":108,"props":174,"children":175},{},[176,181],{"type":18,"tag":137,"props":177,"children":178},{"align":114},[179],{"type":28,"value":180},"host",{"type":18,"tag":137,"props":182,"children":183},{"align":114},[184],{"type":28,"value":185},"主机名",{"type":18,"tag":108,"props":187,"children":188},{},[189,194],{"type":18,"tag":137,"props":190,"children":191},{"align":114},[192],{"type":28,"value":193},"database",{"type":18,"tag":137,"props":195,"children":196},{"align":114},[197],{"type":28,"value":198},"数据库名称",{"type":18,"tag":30,"props":200,"children":201},{},[202],{"type":28,"value":203},"例如，使用PyMySQL模块连接MySQL数据库，示例代码如下：",{"type":18,"tag":205,"props":206,"children":210},"pre",{"code":207,"language":208,"meta":7,"className":209,"style":7},"conn = pymysql.connect(host='localhost',\n                       user='user',\n                       password='passwd',\n                       db='test',\n                       charset='utf8',\n                       cursorclass=pymysql.cursors.DictCursor)\n","python","language-python shiki shiki-themes github-dark",[211],{"type":18,"tag":77,"props":212,"children":213},{"__ignoreMap":7},[214,225,234,243,252,261],{"type":18,"tag":215,"props":216,"children":219},"span",{"class":217,"line":218},"line",1,[220],{"type":18,"tag":215,"props":221,"children":222},{},[223],{"type":28,"value":224},"conn = pymysql.connect(host='localhost',\n",{"type":18,"tag":215,"props":226,"children":228},{"class":217,"line":227},2,[229],{"type":18,"tag":215,"props":230,"children":231},{},[232],{"type":28,"value":233},"                       user='user',\n",{"type":18,"tag":215,"props":235,"children":237},{"class":217,"line":236},3,[238],{"type":18,"tag":215,"props":239,"children":240},{},[241],{"type":28,"value":242},"                       password='passwd',\n",{"type":18,"tag":215,"props":244,"children":246},{"class":217,"line":245},4,[247],{"type":18,"tag":215,"props":248,"children":249},{},[250],{"type":28,"value":251},"                       db='test',\n",{"type":18,"tag":215,"props":253,"children":255},{"class":217,"line":254},5,[256],{"type":18,"tag":215,"props":257,"children":258},{},[259],{"type":28,"value":260},"                       charset='utf8',\n",{"type":18,"tag":215,"props":262,"children":264},{"class":217,"line":263},6,[265],{"type":18,"tag":215,"props":266,"children":267},{},[268],{"type":28,"value":269},"                       cursorclass=pymysql.cursors.DictCursor)\n",{"type":18,"tag":271,"props":272,"children":273},"blockquote",{},[274],{"type":18,"tag":30,"props":275,"children":276},{},[277,279,285],{"type":28,"value":278},"说明：上述代码中，",{"type":18,"tag":77,"props":280,"children":282},{"className":281},[],[283],{"type":28,"value":284},"pymysql.connect()",{"type":28,"value":286},"使用的参数与表1中并不完全相同。在使用时，要以具体的数据库模块为准。",{"type":18,"tag":65,"props":288,"children":290},{"id":289},"_2连接对象的方法",[291],{"type":28,"value":292},"2）连接对象的方法",{"type":18,"tag":30,"props":294,"children":295},{},[296,302,304,309],{"type":18,"tag":77,"props":297,"children":299},{"className":298},[],[300],{"type":28,"value":301},"Connect()",{"type":28,"value":303},"函数",{"type":18,"tag":36,"props":305,"children":306},{},[307],{"type":28,"value":308},"返回连接对象",{"type":28,"value":310},"。这个对象表示目前和数据库的会话，连接对象支持的方法如下表所示。",{"type":18,"tag":100,"props":312,"children":313},{},[314,335],{"type":18,"tag":104,"props":315,"children":316},{},[317],{"type":18,"tag":108,"props":318,"children":319},{},[320,328],{"type":18,"tag":112,"props":321,"children":322},{"align":114},[323],{"type":18,"tag":36,"props":324,"children":325},{},[326],{"type":28,"value":327},"方  法  名",{"type":18,"tag":112,"props":329,"children":330},{"align":114},[331],{"type":18,"tag":36,"props":332,"children":333},{},[334],{"type":28,"value":128},{"type":18,"tag":130,"props":336,"children":337},{},[338,351,364,377],{"type":18,"tag":108,"props":339,"children":340},{},[341,346],{"type":18,"tag":137,"props":342,"children":343},{"align":114},[344],{"type":28,"value":345},"close()",{"type":18,"tag":137,"props":347,"children":348},{"align":114},[349],{"type":28,"value":350},"关闭数据库连接",{"type":18,"tag":108,"props":352,"children":353},{},[354,359],{"type":18,"tag":137,"props":355,"children":356},{"align":114},[357],{"type":28,"value":358},"commit()",{"type":18,"tag":137,"props":360,"children":361},{"align":114},[362],{"type":28,"value":363},"提交事务",{"type":18,"tag":108,"props":365,"children":366},{},[367,372],{"type":18,"tag":137,"props":368,"children":369},{"align":114},[370],{"type":28,"value":371},"rollback()",{"type":18,"tag":137,"props":373,"children":374},{"align":114},[375],{"type":28,"value":376},"回滚事务",{"type":18,"tag":108,"props":378,"children":379},{},[380,385],{"type":18,"tag":137,"props":381,"children":382},{"align":114},[383],{"type":28,"value":384},"cursor()",{"type":18,"tag":137,"props":386,"children":387},{"align":114},[388],{"type":28,"value":389},"获取游标对象，操作数据库，如执行DML操作，调用存储过程等",{"type":18,"tag":30,"props":391,"children":392},{},[393,398],{"type":18,"tag":77,"props":394,"children":396},{"className":395},[],[397],{"type":28,"value":358},{"type":28,"value":399},"方法用于提交事务，事务主要用于处理数据量大、复杂度高的数据。如果操作的是一系列的动作，比如张三给李四转账，有如下2个操作：",{"type":18,"tag":401,"props":402,"children":403},"ul",{},[404,410],{"type":18,"tag":405,"props":406,"children":407},"li",{},[408],{"type":28,"value":409},"张三账户金额减少",{"type":18,"tag":405,"props":411,"children":412},{},[413],{"type":28,"value":414},"李四账户金额增加",{"type":18,"tag":30,"props":416,"children":417},{},[418,420,425,427,432],{"type":28,"value":419},"这时",{"type":18,"tag":36,"props":421,"children":422},{},[423],{"type":28,"value":424},"使用事务",{"type":28,"value":426},"可以",{"type":18,"tag":36,"props":428,"children":429},{},[430],{"type":28,"value":431},"维护数据库的完整性",{"type":28,"value":433},"，保证2个操作要么全部执行，要么全部不执行。",{"type":18,"tag":51,"props":435,"children":437},{"id":436},"游标对象",[438],{"type":28,"value":436},{"type":18,"tag":30,"props":440,"children":441},{},[442],{"type":18,"tag":36,"props":443,"children":444},{},[445],{"type":28,"value":446},"游标对象（Cursor Object）代表数据库中的游标，用于指示抓取数据操作的上下文，主要提供执行SQL语句、调用存储过程、获取查询结果等方法。",{"type":18,"tag":30,"props":448,"children":449},{},[450],{"type":28,"value":451},"如何获取游标对象呢？通过使用连接对象的cursor()方法，可以获取到游标对象。游标对象的属性如下所示：",{"type":18,"tag":401,"props":453,"children":454},{},[455,466],{"type":18,"tag":405,"props":456,"children":457},{},[458,464],{"type":18,"tag":77,"props":459,"children":461},{"className":460},[],[462],{"type":28,"value":463},"description",{"type":28,"value":465},"：数据库列类型和值的描述信息。",{"type":18,"tag":405,"props":467,"children":468},{},[469,475],{"type":18,"tag":77,"props":470,"children":472},{"className":471},[],[473],{"type":28,"value":474},"rowcount",{"type":28,"value":476},"：回返结果的行数统计信息，如SELECT、UPDATE、CALLPROC等。",{"type":18,"tag":30,"props":478,"children":479},{},[480],{"type":28,"value":481},"游标对象的方法如下表所示。",{"type":18,"tag":100,"props":483,"children":484},{},[485,505],{"type":18,"tag":104,"props":486,"children":487},{},[488],{"type":18,"tag":108,"props":489,"children":490},{},[491,498],{"type":18,"tag":112,"props":492,"children":493},{"align":114},[494],{"type":18,"tag":36,"props":495,"children":496},{},[497],{"type":28,"value":327},{"type":18,"tag":112,"props":499,"children":500},{"align":114},[501],{"type":18,"tag":36,"props":502,"children":503},{},[504],{"type":28,"value":128},{"type":18,"tag":130,"props":506,"children":507},{},[508,528,540,558,571,584,597,610,623,636,649],{"type":18,"tag":108,"props":509,"children":510},{},[511,523],{"type":18,"tag":137,"props":512,"children":513},{"align":114},[514,516,521],{"type":28,"value":515},"callproc(procname,",{"type":18,"tag":215,"props":517,"children":518},{},[519],{"type":28,"value":520},", parameters",{"type":28,"value":522},")",{"type":18,"tag":137,"props":524,"children":525},{"align":114},[526],{"type":28,"value":527},"调用存储过程，需要数据库支持",{"type":18,"tag":108,"props":529,"children":530},{},[531,535],{"type":18,"tag":137,"props":532,"children":533},{"align":114},[534],{"type":28,"value":345},{"type":18,"tag":137,"props":536,"children":537},{"align":114},[538],{"type":28,"value":539},"关闭当前游标",{"type":18,"tag":108,"props":541,"children":542},{},[543,553],{"type":18,"tag":137,"props":544,"children":545},{"align":114},[546,548,552],{"type":28,"value":547},"execute(operation",{"type":18,"tag":215,"props":549,"children":550},{},[551],{"type":28,"value":520},{"type":28,"value":522},{"type":18,"tag":137,"props":554,"children":555},{"align":114},[556],{"type":28,"value":557},"执行数据库操作，SQL语句或者数据库命令",{"type":18,"tag":108,"props":559,"children":560},{},[561,566],{"type":18,"tag":137,"props":562,"children":563},{"align":114},[564],{"type":28,"value":565},"executemany(operation, seq_of_params)",{"type":18,"tag":137,"props":567,"children":568},{"align":114},[569],{"type":28,"value":570},"用于批量操作，如批量更新",{"type":18,"tag":108,"props":572,"children":573},{},[574,579],{"type":18,"tag":137,"props":575,"children":576},{"align":114},[577],{"type":28,"value":578},"fetchone()",{"type":18,"tag":137,"props":580,"children":581},{"align":114},[582],{"type":28,"value":583},"获取查询结果集中的下一条记录",{"type":18,"tag":108,"props":585,"children":586},{},[587,592],{"type":18,"tag":137,"props":588,"children":589},{"align":114},[590],{"type":28,"value":591},"fetchmany(size)",{"type":18,"tag":137,"props":593,"children":594},{"align":114},[595],{"type":28,"value":596},"获取指定数量的记录",{"type":18,"tag":108,"props":598,"children":599},{},[600,605],{"type":18,"tag":137,"props":601,"children":602},{"align":114},[603],{"type":28,"value":604},"fetchall()",{"type":18,"tag":137,"props":606,"children":607},{"align":114},[608],{"type":28,"value":609},"获取结果集的所有记录",{"type":18,"tag":108,"props":611,"children":612},{},[613,618],{"type":18,"tag":137,"props":614,"children":615},{"align":114},[616],{"type":28,"value":617},"nextset()",{"type":18,"tag":137,"props":619,"children":620},{"align":114},[621],{"type":28,"value":622},"跳至下一个可用的结果集",{"type":18,"tag":108,"props":624,"children":625},{},[626,631],{"type":18,"tag":137,"props":627,"children":628},{"align":114},[629],{"type":28,"value":630},"arraysize",{"type":18,"tag":137,"props":632,"children":633},{"align":114},[634],{"type":28,"value":635},"指定使用fetchmany()获取的行数，默认为1",{"type":18,"tag":108,"props":637,"children":638},{},[639,644],{"type":18,"tag":137,"props":640,"children":641},{"align":114},[642],{"type":28,"value":643},"setinputsizes(sizes)",{"type":18,"tag":137,"props":645,"children":646},{"align":114},[647],{"type":28,"value":648},"设置在调用execute*()方法时分配的内存区域大小",{"type":18,"tag":108,"props":650,"children":651},{},[652,657],{"type":18,"tag":137,"props":653,"children":654},{"align":114},[655],{"type":28,"value":656},"setoutputsize(sizes)",{"type":18,"tag":137,"props":658,"children":659},{"align":114},[660],{"type":28,"value":661},"设置列缓冲区大小，对大数据列（如LONGS和BLOBS）尤其有用",{"type":18,"tag":23,"props":663,"children":665},{"id":664},"使用sqlite",[666],{"type":28,"value":667},"使用SQLite",{"type":18,"tag":30,"props":669,"children":670},{},[671],{"type":28,"value":672},"与许多其他数据库管理系统不同，SQLite不是一个客户端\u002F服务器结构的数据库引擎，而是一种嵌入式数据库，它的数据库就是一个文件。SQLite将整个数据库，包括定义、表、索引以及数据本身，作为一个单独的、可跨平台使用的文件存储在主机中。由于SQLite本身是C写的，而且体积很小，所以，经常被集成到各种应用程序中。Python就内置了SQLite3，所以在Python中使用SQLite，不需要安装任何模块，直接使用。",{"type":18,"tag":51,"props":674,"children":676},{"id":675},"创建数据库文件",[677],{"type":28,"value":675},{"type":18,"tag":30,"props":679,"children":680},{},[681],{"type":28,"value":682},"由于Python中已经内置了SQLite3，所以可以直接使用import语句导入SQLite3模块。Python操作数据库的通用的流程如图所示。",{"type":18,"tag":30,"props":684,"children":685},{},[686],{"type":18,"tag":687,"props":688,"children":691},"img",{"alt":689,"src":690},"Pasted image 20250721100354","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250721100354.png",[],{"type":18,"tag":30,"props":693,"children":694},{},[695],{"type":28,"value":696},"示例：创建一个info.db的数据库文件，然后执行SQL语句创建一个user（用户表），user表包含id和name两个字段。具体代码如下：",{"type":18,"tag":205,"props":698,"children":700},{"code":699,"language":208,"meta":7,"className":209,"style":7},"import sqlite3\n\n# 连接到SQLite数据库\n# 数据库文件是info.db，如果文件不存在，会自动在当前目录创建\nconn = sqlite3.connect('info.db')\n\n# 创建一个Cursor\ncursor = conn.cursor()\n\n# 执行一条SQL语句，创建user表                   \ncursor.execute('create  table  user (id int(10)  primary key, name varchar(20))')\n\n# 关闭游标\ncursor.close()\n\n# 关闭Connection\nconn.close()\n",[701],{"type":18,"tag":77,"props":702,"children":703},{"__ignoreMap":7},[704,712,721,729,737,745,752,761,770,778,787,796,804,813,822,830,839],{"type":18,"tag":215,"props":705,"children":706},{"class":217,"line":218},[707],{"type":18,"tag":215,"props":708,"children":709},{},[710],{"type":28,"value":711},"import sqlite3\n",{"type":18,"tag":215,"props":713,"children":714},{"class":217,"line":227},[715],{"type":18,"tag":215,"props":716,"children":718},{"emptyLinePlaceholder":717},true,[719],{"type":28,"value":720},"\n",{"type":18,"tag":215,"props":722,"children":723},{"class":217,"line":236},[724],{"type":18,"tag":215,"props":725,"children":726},{},[727],{"type":28,"value":728},"# 连接到SQLite数据库\n",{"type":18,"tag":215,"props":730,"children":731},{"class":217,"line":245},[732],{"type":18,"tag":215,"props":733,"children":734},{},[735],{"type":28,"value":736},"# 数据库文件是info.db，如果文件不存在，会自动在当前目录创建\n",{"type":18,"tag":215,"props":738,"children":739},{"class":217,"line":254},[740],{"type":18,"tag":215,"props":741,"children":742},{},[743],{"type":28,"value":744},"conn = sqlite3.connect('info.db')\n",{"type":18,"tag":215,"props":746,"children":747},{"class":217,"line":263},[748],{"type":18,"tag":215,"props":749,"children":750},{"emptyLinePlaceholder":717},[751],{"type":28,"value":720},{"type":18,"tag":215,"props":753,"children":755},{"class":217,"line":754},7,[756],{"type":18,"tag":215,"props":757,"children":758},{},[759],{"type":28,"value":760},"# 创建一个Cursor\n",{"type":18,"tag":215,"props":762,"children":764},{"class":217,"line":763},8,[765],{"type":18,"tag":215,"props":766,"children":767},{},[768],{"type":28,"value":769},"cursor = conn.cursor()\n",{"type":18,"tag":215,"props":771,"children":773},{"class":217,"line":772},9,[774],{"type":18,"tag":215,"props":775,"children":776},{"emptyLinePlaceholder":717},[777],{"type":28,"value":720},{"type":18,"tag":215,"props":779,"children":781},{"class":217,"line":780},10,[782],{"type":18,"tag":215,"props":783,"children":784},{},[785],{"type":28,"value":786},"# 执行一条SQL语句，创建user表                   \n",{"type":18,"tag":215,"props":788,"children":790},{"class":217,"line":789},11,[791],{"type":18,"tag":215,"props":792,"children":793},{},[794],{"type":28,"value":795},"cursor.execute('create  table  user (id int(10)  primary key, name varchar(20))')\n",{"type":18,"tag":215,"props":797,"children":799},{"class":217,"line":798},12,[800],{"type":18,"tag":215,"props":801,"children":802},{"emptyLinePlaceholder":717},[803],{"type":28,"value":720},{"type":18,"tag":215,"props":805,"children":807},{"class":217,"line":806},13,[808],{"type":18,"tag":215,"props":809,"children":810},{},[811],{"type":28,"value":812},"# 关闭游标\n",{"type":18,"tag":215,"props":814,"children":816},{"class":217,"line":815},14,[817],{"type":18,"tag":215,"props":818,"children":819},{},[820],{"type":28,"value":821},"cursor.close()\n",{"type":18,"tag":215,"props":823,"children":825},{"class":217,"line":824},15,[826],{"type":18,"tag":215,"props":827,"children":828},{"emptyLinePlaceholder":717},[829],{"type":28,"value":720},{"type":18,"tag":215,"props":831,"children":833},{"class":217,"line":832},16,[834],{"type":18,"tag":215,"props":835,"children":836},{},[837],{"type":28,"value":838},"# 关闭Connection\n",{"type":18,"tag":215,"props":840,"children":842},{"class":217,"line":841},17,[843],{"type":18,"tag":215,"props":844,"children":845},{},[846],{"type":28,"value":847},"conn.close()\n",{"type":18,"tag":30,"props":849,"children":850},{},[851,853,859],{"type":28,"value":852},"上述代码中，使用",{"type":18,"tag":77,"props":854,"children":856},{"className":855},[],[857],{"type":28,"value":858},"sqlite3.connect()",{"type":28,"value":860},"方法连接SQLite数据库文件info.db，由于info.db文件并不存在，所以会在本实例Python代码同级目录下创建info.db文件，该文件包含了user表的相关信息。info.db文件所在目录如图所示。",{"type":18,"tag":30,"props":862,"children":863},{},[864],{"type":18,"tag":687,"props":865,"children":868},{"alt":866,"src":867},"Pasted image 20250721101112","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250721101112.png",[],{"type":18,"tag":271,"props":870,"children":871},{},[872],{"type":18,"tag":30,"props":873,"children":874},{},[875,877,883],{"type":28,"value":876},"说明：再次运行示例时，会提示错误信息：",{"type":18,"tag":77,"props":878,"children":880},{"className":879},[],[881],{"type":28,"value":882},"sqlite3.OperationalError:table user alread exists",{"type":28,"value":884},"，这是因为user表已经存在。",{"type":18,"tag":51,"props":886,"children":888},{"id":887},"操作sqlite",[889],{"type":28,"value":890},"操作SQLite",{"type":18,"tag":65,"props":892,"children":894},{"id":893},"_1新增用户数据信息",[895],{"type":28,"value":896},"1）新增用户数据信息",{"type":18,"tag":30,"props":898,"children":899},{},[900],{"type":28,"value":901},"为了向数据表中新增数据，可以使用如下SQL语句：",{"type":18,"tag":205,"props":903,"children":907},{"code":904,"language":905,"meta":7,"className":906,"style":7},"insert into 表名(字段名1,字段名2,…,字段名n)  values (字段值1,字段值2,…,字段值n)\n","sql","language-sql shiki shiki-themes github-dark",[908],{"type":18,"tag":77,"props":909,"children":910},{"__ignoreMap":7},[911],{"type":18,"tag":215,"props":912,"children":913},{"class":217,"line":218},[914],{"type":18,"tag":215,"props":915,"children":916},{},[917],{"type":28,"value":904},{"type":18,"tag":30,"props":919,"children":920},{},[921,923,929,931,937,939,944],{"type":28,"value":922},"在user表中，有2个字段，字段名分别为",{"type":18,"tag":77,"props":924,"children":926},{"className":925},[],[927],{"type":28,"value":928},"id",{"type":28,"value":930},"和",{"type":18,"tag":77,"props":932,"children":934},{"className":933},[],[935],{"type":28,"value":936},"name",{"type":28,"value":938},"。而字段值需要根据字段的数据类型来赋值，如id是一个长度为10的整型，",{"type":18,"tag":77,"props":940,"children":942},{"className":941},[],[943],{"type":28,"value":936},{"type":28,"value":945},"是长度为20的字符串型数据。向user表中插入3条用户信息记录，则SQL语句如下：",{"type":18,"tag":205,"props":947,"children":949},{"code":948,"language":208,"meta":7,"className":209,"style":7},"cursor.execute('insert into user (id, name) values (\"1\", \"xiaozhang\")')\ncursor.execute('insert into user (id, name) values (\"2\", \"xiaolan\")')\ncursor.execute('insert into user (id, name) values (\"3\", \"xiaoliu\")')\n",[950],{"type":18,"tag":77,"props":951,"children":952},{"__ignoreMap":7},[953,961,969],{"type":18,"tag":215,"props":954,"children":955},{"class":217,"line":218},[956],{"type":18,"tag":215,"props":957,"children":958},{},[959],{"type":28,"value":960},"cursor.execute('insert into user (id, name) values (\"1\", \"xiaozhang\")')\n",{"type":18,"tag":215,"props":962,"children":963},{"class":217,"line":227},[964],{"type":18,"tag":215,"props":965,"children":966},{},[967],{"type":28,"value":968},"cursor.execute('insert into user (id, name) values (\"2\", \"xiaolan\")')\n",{"type":18,"tag":215,"props":970,"children":971},{"class":217,"line":236},[972],{"type":18,"tag":215,"props":973,"children":974},{},[975],{"type":28,"value":976},"cursor.execute('insert into user (id, name) values (\"3\", \"xiaoliu\")')\n",{"type":18,"tag":30,"props":978,"children":979},{},[980],{"type":28,"value":981},"下面通过一个实例介绍向SQLite数据库中插入数据的流程。",{"type":18,"tag":30,"props":983,"children":984},{},[985],{"type":18,"tag":36,"props":986,"children":987},{},[988],{"type":28,"value":989},"新增用户数据信息",{"type":18,"tag":30,"props":991,"children":992},{},[993,995,1000,1002,1007,1009,1014],{"type":28,"value":994},"由于在示例中已经创建了user表，所以本实例可以直接操作user表，向user表中插入3条用户信息。此外，由于是新增数据，需要使用",{"type":18,"tag":77,"props":996,"children":998},{"className":997},[],[999],{"type":28,"value":358},{"type":28,"value":1001},"方法提交事务。因为对于增加、修改和删除操作，使用",{"type":18,"tag":77,"props":1003,"children":1005},{"className":1004},[],[1006],{"type":28,"value":358},{"type":28,"value":1008},"方法提交事务后，如果相应操作失败，可以使用",{"type":18,"tag":77,"props":1010,"children":1012},{"className":1011},[],[1013],{"type":28,"value":371},{"type":28,"value":1015},"方法回滚到操作之前的状态。新增用户数据信息具体代码如下：",{"type":18,"tag":205,"props":1017,"children":1019},{"code":1018,"language":208,"meta":7,"className":209,"style":7},"import sqlite3\n\n# 连接到SQLite数据库\n# 数据库文件是info.db\n# 如果文件不存在，会自动在当前目录创建\nconn = sqlite3.connect('info.db')\n\n# 创建一个Cursor\ncursor = conn.cursor()\n\n# 执行一条SQL语句，插入一条记录\ncursor.execute('insert into user (id, name) values (\"1\", \"xiaozhang\")')\ncursor.execute('insert into user (id, name) values (\"2\", \"xiaolan\")')\ncursor.execute('insert into user (id, name) values (\"3\", \"xiaoliu\")')\n\n# 关闭游标\ncursor.close()\n\n# 提交事务\nconn.commit()\n\n# 关闭Connection\nconn.close()\n",[1020],{"type":18,"tag":77,"props":1021,"children":1022},{"__ignoreMap":7},[1023,1030,1037,1044,1052,1060,1067,1074,1081,1088,1095,1103,1110,1117,1124,1131,1138,1145,1153,1162,1171,1179,1187],{"type":18,"tag":215,"props":1024,"children":1025},{"class":217,"line":218},[1026],{"type":18,"tag":215,"props":1027,"children":1028},{},[1029],{"type":28,"value":711},{"type":18,"tag":215,"props":1031,"children":1032},{"class":217,"line":227},[1033],{"type":18,"tag":215,"props":1034,"children":1035},{"emptyLinePlaceholder":717},[1036],{"type":28,"value":720},{"type":18,"tag":215,"props":1038,"children":1039},{"class":217,"line":236},[1040],{"type":18,"tag":215,"props":1041,"children":1042},{},[1043],{"type":28,"value":728},{"type":18,"tag":215,"props":1045,"children":1046},{"class":217,"line":245},[1047],{"type":18,"tag":215,"props":1048,"children":1049},{},[1050],{"type":28,"value":1051},"# 数据库文件是info.db\n",{"type":18,"tag":215,"props":1053,"children":1054},{"class":217,"line":254},[1055],{"type":18,"tag":215,"props":1056,"children":1057},{},[1058],{"type":28,"value":1059},"# 如果文件不存在，会自动在当前目录创建\n",{"type":18,"tag":215,"props":1061,"children":1062},{"class":217,"line":263},[1063],{"type":18,"tag":215,"props":1064,"children":1065},{},[1066],{"type":28,"value":744},{"type":18,"tag":215,"props":1068,"children":1069},{"class":217,"line":754},[1070],{"type":18,"tag":215,"props":1071,"children":1072},{"emptyLinePlaceholder":717},[1073],{"type":28,"value":720},{"type":18,"tag":215,"props":1075,"children":1076},{"class":217,"line":763},[1077],{"type":18,"tag":215,"props":1078,"children":1079},{},[1080],{"type":28,"value":760},{"type":18,"tag":215,"props":1082,"children":1083},{"class":217,"line":772},[1084],{"type":18,"tag":215,"props":1085,"children":1086},{},[1087],{"type":28,"value":769},{"type":18,"tag":215,"props":1089,"children":1090},{"class":217,"line":780},[1091],{"type":18,"tag":215,"props":1092,"children":1093},{"emptyLinePlaceholder":717},[1094],{"type":28,"value":720},{"type":18,"tag":215,"props":1096,"children":1097},{"class":217,"line":789},[1098],{"type":18,"tag":215,"props":1099,"children":1100},{},[1101],{"type":28,"value":1102},"# 执行一条SQL语句，插入一条记录\n",{"type":18,"tag":215,"props":1104,"children":1105},{"class":217,"line":798},[1106],{"type":18,"tag":215,"props":1107,"children":1108},{},[1109],{"type":28,"value":960},{"type":18,"tag":215,"props":1111,"children":1112},{"class":217,"line":806},[1113],{"type":18,"tag":215,"props":1114,"children":1115},{},[1116],{"type":28,"value":968},{"type":18,"tag":215,"props":1118,"children":1119},{"class":217,"line":815},[1120],{"type":18,"tag":215,"props":1121,"children":1122},{},[1123],{"type":28,"value":976},{"type":18,"tag":215,"props":1125,"children":1126},{"class":217,"line":824},[1127],{"type":18,"tag":215,"props":1128,"children":1129},{"emptyLinePlaceholder":717},[1130],{"type":28,"value":720},{"type":18,"tag":215,"props":1132,"children":1133},{"class":217,"line":832},[1134],{"type":18,"tag":215,"props":1135,"children":1136},{},[1137],{"type":28,"value":812},{"type":18,"tag":215,"props":1139,"children":1140},{"class":217,"line":841},[1141],{"type":18,"tag":215,"props":1142,"children":1143},{},[1144],{"type":28,"value":821},{"type":18,"tag":215,"props":1146,"children":1148},{"class":217,"line":1147},18,[1149],{"type":18,"tag":215,"props":1150,"children":1151},{"emptyLinePlaceholder":717},[1152],{"type":28,"value":720},{"type":18,"tag":215,"props":1154,"children":1156},{"class":217,"line":1155},19,[1157],{"type":18,"tag":215,"props":1158,"children":1159},{},[1160],{"type":28,"value":1161},"# 提交事务\n",{"type":18,"tag":215,"props":1163,"children":1165},{"class":217,"line":1164},20,[1166],{"type":18,"tag":215,"props":1167,"children":1168},{},[1169],{"type":28,"value":1170},"conn.commit()\n",{"type":18,"tag":215,"props":1172,"children":1174},{"class":217,"line":1173},21,[1175],{"type":18,"tag":215,"props":1176,"children":1177},{"emptyLinePlaceholder":717},[1178],{"type":28,"value":720},{"type":18,"tag":215,"props":1180,"children":1182},{"class":217,"line":1181},22,[1183],{"type":18,"tag":215,"props":1184,"children":1185},{},[1186],{"type":28,"value":838},{"type":18,"tag":215,"props":1188,"children":1190},{"class":217,"line":1189},23,[1191],{"type":18,"tag":215,"props":1192,"children":1193},{},[1194],{"type":28,"value":847},{"type":18,"tag":30,"props":1196,"children":1197},{},[1198],{"type":28,"value":1199},"运行该实例，会向user表中插入3条记录。为验证程序是否正常运行，可以再次运行，如果提示如下信息，说明插入成功（因为user表中已经保存了上一次插入的记录，所以再次插入会报错）。",{"type":18,"tag":205,"props":1201,"children":1203},{"code":1202},"sqlite3.IntegrityError: UNIQUE constraint failed: user.id\n",[1204],{"type":18,"tag":77,"props":1205,"children":1206},{"__ignoreMap":7},[1207],{"type":28,"value":1202},{"type":18,"tag":65,"props":1209,"children":1211},{"id":1210},"_2查看用户数据信息",[1212],{"type":28,"value":1213},"2）查看用户数据信息",{"type":18,"tag":30,"props":1215,"children":1216},{},[1217],{"type":28,"value":1218},"查找user表中的数据可以使用如下SQL语句：",{"type":18,"tag":205,"props":1220,"children":1222},{"code":1221,"language":905,"meta":7,"className":906,"style":7},"select  字段名1,字段名2,字段名3,… from 表名  where 查询条件\n",[1223],{"type":18,"tag":77,"props":1224,"children":1225},{"__ignoreMap":7},[1226],{"type":18,"tag":215,"props":1227,"children":1228},{"class":217,"line":218},[1229],{"type":18,"tag":215,"props":1230,"children":1231},{},[1232],{"type":28,"value":1221},{"type":18,"tag":30,"props":1234,"children":1235},{},[1236],{"type":28,"value":1237},"查看用户信息的代码与插入数据信息大致相同，不同点在于使用的SQL语句不同。此外，查询数据时通常使用如下3种方式：",{"type":18,"tag":401,"props":1239,"children":1240},{},[1241,1251,1261],{"type":18,"tag":405,"props":1242,"children":1243},{},[1244,1249],{"type":18,"tag":77,"props":1245,"children":1247},{"className":1246},[],[1248],{"type":28,"value":578},{"type":28,"value":1250},"：获取查询结果集中的下一条记录。",{"type":18,"tag":405,"props":1252,"children":1253},{},[1254,1259],{"type":18,"tag":77,"props":1255,"children":1257},{"className":1256},[],[1258],{"type":28,"value":591},{"type":28,"value":1260},"：获取指定数量的记录。",{"type":18,"tag":405,"props":1262,"children":1263},{},[1264,1269],{"type":18,"tag":77,"props":1265,"children":1267},{"className":1266},[],[1268],{"type":28,"value":604},{"type":28,"value":1270},"：获取结果集的所有记录。",{"type":18,"tag":30,"props":1272,"children":1273},{},[1274],{"type":28,"value":1275},"下面通过一个实例来学习这3种查询方式的区别。",{"type":18,"tag":30,"props":1277,"children":1278},{},[1279],{"type":18,"tag":36,"props":1280,"children":1281},{},[1282],{"type":28,"value":1283},"使用3种方式查询用户数据信息",{"type":18,"tag":30,"props":1285,"children":1286},{},[1287,1289,1295,1297,1303,1304,1310],{"type":28,"value":1288},"1）分别使用",{"type":18,"tag":77,"props":1290,"children":1292},{"className":1291},[],[1293],{"type":28,"value":1294},"fetchone",{"type":28,"value":1296},"、",{"type":18,"tag":77,"props":1298,"children":1300},{"className":1299},[],[1301],{"type":28,"value":1302},"fetchmany",{"type":28,"value":930},{"type":18,"tag":77,"props":1305,"children":1307},{"className":1306},[],[1308],{"type":28,"value":1309},"fetchall",{"type":28,"value":1311},"这3种方式查询用户信息，具体代码如下：",{"type":18,"tag":205,"props":1313,"children":1315},{"code":1314,"language":208,"meta":7,"className":209,"style":7},"import sqlite3\n\n# 连接到SQLite数据库,数据库文件是info.db\nconn = sqlite3.connect('info.db')\n\n# 创建一个Cursor\ncursor = conn.cursor()\n\n# 执行查询语句\ncursor.execute('select * from user')\n\n# 获取查询结果\nresult1 = cursor.fetchone()\nprint(result1)\n\n# 关闭游标\ncursor.close()\n\n# 关闭Connection\nconn.close()\n",[1316],{"type":18,"tag":77,"props":1317,"children":1318},{"__ignoreMap":7},[1319,1326,1333,1341,1348,1355,1362,1369,1376,1384,1392,1399,1407,1415,1423,1430,1437,1444,1451,1458],{"type":18,"tag":215,"props":1320,"children":1321},{"class":217,"line":218},[1322],{"type":18,"tag":215,"props":1323,"children":1324},{},[1325],{"type":28,"value":711},{"type":18,"tag":215,"props":1327,"children":1328},{"class":217,"line":227},[1329],{"type":18,"tag":215,"props":1330,"children":1331},{"emptyLinePlaceholder":717},[1332],{"type":28,"value":720},{"type":18,"tag":215,"props":1334,"children":1335},{"class":217,"line":236},[1336],{"type":18,"tag":215,"props":1337,"children":1338},{},[1339],{"type":28,"value":1340},"# 连接到SQLite数据库,数据库文件是info.db\n",{"type":18,"tag":215,"props":1342,"children":1343},{"class":217,"line":245},[1344],{"type":18,"tag":215,"props":1345,"children":1346},{},[1347],{"type":28,"value":744},{"type":18,"tag":215,"props":1349,"children":1350},{"class":217,"line":254},[1351],{"type":18,"tag":215,"props":1352,"children":1353},{"emptyLinePlaceholder":717},[1354],{"type":28,"value":720},{"type":18,"tag":215,"props":1356,"children":1357},{"class":217,"line":263},[1358],{"type":18,"tag":215,"props":1359,"children":1360},{},[1361],{"type":28,"value":760},{"type":18,"tag":215,"props":1363,"children":1364},{"class":217,"line":754},[1365],{"type":18,"tag":215,"props":1366,"children":1367},{},[1368],{"type":28,"value":769},{"type":18,"tag":215,"props":1370,"children":1371},{"class":217,"line":763},[1372],{"type":18,"tag":215,"props":1373,"children":1374},{"emptyLinePlaceholder":717},[1375],{"type":28,"value":720},{"type":18,"tag":215,"props":1377,"children":1378},{"class":217,"line":772},[1379],{"type":18,"tag":215,"props":1380,"children":1381},{},[1382],{"type":28,"value":1383},"# 执行查询语句\n",{"type":18,"tag":215,"props":1385,"children":1386},{"class":217,"line":780},[1387],{"type":18,"tag":215,"props":1388,"children":1389},{},[1390],{"type":28,"value":1391},"cursor.execute('select * from user')\n",{"type":18,"tag":215,"props":1393,"children":1394},{"class":217,"line":789},[1395],{"type":18,"tag":215,"props":1396,"children":1397},{"emptyLinePlaceholder":717},[1398],{"type":28,"value":720},{"type":18,"tag":215,"props":1400,"children":1401},{"class":217,"line":798},[1402],{"type":18,"tag":215,"props":1403,"children":1404},{},[1405],{"type":28,"value":1406},"# 获取查询结果\n",{"type":18,"tag":215,"props":1408,"children":1409},{"class":217,"line":806},[1410],{"type":18,"tag":215,"props":1411,"children":1412},{},[1413],{"type":28,"value":1414},"result1 = cursor.fetchone()\n",{"type":18,"tag":215,"props":1416,"children":1417},{"class":217,"line":815},[1418],{"type":18,"tag":215,"props":1419,"children":1420},{},[1421],{"type":28,"value":1422},"print(result1)\n",{"type":18,"tag":215,"props":1424,"children":1425},{"class":217,"line":824},[1426],{"type":18,"tag":215,"props":1427,"children":1428},{"emptyLinePlaceholder":717},[1429],{"type":28,"value":720},{"type":18,"tag":215,"props":1431,"children":1432},{"class":217,"line":832},[1433],{"type":18,"tag":215,"props":1434,"children":1435},{},[1436],{"type":28,"value":812},{"type":18,"tag":215,"props":1438,"children":1439},{"class":217,"line":841},[1440],{"type":18,"tag":215,"props":1441,"children":1442},{},[1443],{"type":28,"value":821},{"type":18,"tag":215,"props":1445,"children":1446},{"class":217,"line":1147},[1447],{"type":18,"tag":215,"props":1448,"children":1449},{"emptyLinePlaceholder":717},[1450],{"type":28,"value":720},{"type":18,"tag":215,"props":1452,"children":1453},{"class":217,"line":1155},[1454],{"type":18,"tag":215,"props":1455,"children":1456},{},[1457],{"type":28,"value":838},{"type":18,"tag":215,"props":1459,"children":1460},{"class":217,"line":1164},[1461],{"type":18,"tag":215,"props":1462,"children":1463},{},[1464],{"type":28,"value":847},{"type":18,"tag":30,"props":1466,"children":1467},{},[1468,1470,1475,1477,1483],{"type":28,"value":1469},"使用",{"type":18,"tag":77,"props":1471,"children":1473},{"className":1472},[],[1474],{"type":28,"value":578},{"type":28,"value":1476},"方法返回的",{"type":18,"tag":77,"props":1478,"children":1480},{"className":1479},[],[1481],{"type":28,"value":1482},"result1",{"type":28,"value":1484},"为一个元组，执行结果如下：",{"type":18,"tag":205,"props":1486,"children":1488},{"code":1487},"(1, 'xiaozhang')\n",[1489],{"type":18,"tag":77,"props":1490,"children":1491},{"__ignoreMap":7},[1492],{"type":28,"value":1487},{"type":18,"tag":30,"props":1494,"children":1495},{},[1496],{"type":28,"value":1497},"2）修改实例代码，将获取查询结果的语句块代码修改为：",{"type":18,"tag":205,"props":1499,"children":1501},{"code":1500,"language":208,"meta":7,"className":209,"style":7},"result2 = cursor.fetchmany(2)     # 使用fetchmany方法查询多条数据\nprint(result2)\n",[1502],{"type":18,"tag":77,"props":1503,"children":1504},{"__ignoreMap":7},[1505,1513],{"type":18,"tag":215,"props":1506,"children":1507},{"class":217,"line":218},[1508],{"type":18,"tag":215,"props":1509,"children":1510},{},[1511],{"type":28,"value":1512},"result2 = cursor.fetchmany(2)     # 使用fetchmany方法查询多条数据\n",{"type":18,"tag":215,"props":1514,"children":1515},{"class":217,"line":227},[1516],{"type":18,"tag":215,"props":1517,"children":1518},{},[1519],{"type":28,"value":1520},"print(result2)\n",{"type":18,"tag":30,"props":1522,"children":1523},{},[1524,1525,1531,1533,1539],{"type":28,"value":1469},{"type":18,"tag":77,"props":1526,"children":1528},{"className":1527},[],[1529],{"type":28,"value":1530},"fetchmany()",{"type":28,"value":1532},"方法传递一个参数，其值为2，默认为1。返回的",{"type":18,"tag":77,"props":1534,"children":1536},{"className":1535},[],[1537],{"type":28,"value":1538},"result2",{"type":28,"value":1540},"为一个列表，列表中包含2个元组，运行结果如下：",{"type":18,"tag":205,"props":1542,"children":1544},{"code":1543},"[(1, 'xiaozhang'), (2, 'xiaolan')]\n",[1545],{"type":18,"tag":77,"props":1546,"children":1547},{"__ignoreMap":7},[1548],{"type":28,"value":1543},{"type":18,"tag":30,"props":1550,"children":1551},{},[1552],{"type":28,"value":1553},"3）修改实例代码，将获取查询结果的语句块代码修改为：",{"type":18,"tag":205,"props":1555,"children":1557},{"code":1556,"language":208,"meta":7,"className":209,"style":7},"result3 = cursor.fetchall()         # 使用fetchmany方法查询多条数据\nprint(result3)\n",[1558],{"type":18,"tag":77,"props":1559,"children":1560},{"__ignoreMap":7},[1561,1569],{"type":18,"tag":215,"props":1562,"children":1563},{"class":217,"line":218},[1564],{"type":18,"tag":215,"props":1565,"children":1566},{},[1567],{"type":28,"value":1568},"result3 = cursor.fetchall()         # 使用fetchmany方法查询多条数据\n",{"type":18,"tag":215,"props":1570,"children":1571},{"class":217,"line":227},[1572],{"type":18,"tag":215,"props":1573,"children":1574},{},[1575],{"type":28,"value":1576},"print(result3)\n",{"type":18,"tag":30,"props":1578,"children":1579},{},[1580,1581,1586,1587,1593],{"type":28,"value":1469},{"type":18,"tag":77,"props":1582,"children":1584},{"className":1583},[],[1585],{"type":28,"value":604},{"type":28,"value":1476},{"type":18,"tag":77,"props":1588,"children":1590},{"className":1589},[],[1591],{"type":28,"value":1592},"result3",{"type":28,"value":1594},"为一个列表，列表中包含所有user表中数据组成的元组，运行结果如下：",{"type":18,"tag":205,"props":1596,"children":1598},{"code":1597},"[(1, 'xiaozhang'), (2, 'xiaolan'), (3, 'xiaoliu')]\n",[1599],{"type":18,"tag":77,"props":1600,"children":1601},{"__ignoreMap":7},[1602],{"type":28,"value":1597},{"type":18,"tag":30,"props":1604,"children":1605},{},[1606],{"type":28,"value":1607},"4）修改实例代码，将获取查询结果的语句块代码修改为：",{"type":18,"tag":205,"props":1609,"children":1611},{"code":1610,"language":208,"meta":7,"className":209,"style":7},"cursor.execute('select * from user where id > ?',(1,))\nresult3 = cursor.fetchall()\nprint(result3)\n",[1612],{"type":18,"tag":77,"props":1613,"children":1614},{"__ignoreMap":7},[1615,1623,1631],{"type":18,"tag":215,"props":1616,"children":1617},{"class":217,"line":218},[1618],{"type":18,"tag":215,"props":1619,"children":1620},{},[1621],{"type":28,"value":1622},"cursor.execute('select * from user where id > ?',(1,))\n",{"type":18,"tag":215,"props":1624,"children":1625},{"class":217,"line":227},[1626],{"type":18,"tag":215,"props":1627,"children":1628},{},[1629],{"type":28,"value":1630},"result3 = cursor.fetchall()\n",{"type":18,"tag":215,"props":1632,"children":1633},{"class":217,"line":236},[1634],{"type":18,"tag":215,"props":1635,"children":1636},{},[1637],{"type":28,"value":1576},{"type":18,"tag":30,"props":1639,"children":1640},{},[1641],{"type":28,"value":1642},"在select查询语句中，使用问号作为占位符代替具体的数值，然后使用一个元组来替换问号（注意，不要忽略元组中最后的逗号），元组类似于传参会将值传给占位符。上述查询语句等价于：",{"type":18,"tag":205,"props":1644,"children":1646},{"code":1645,"language":208,"meta":7,"className":209,"style":7},"cursor.execute('select * from user where id > 1')\n",[1647],{"type":18,"tag":77,"props":1648,"children":1649},{"__ignoreMap":7},[1650],{"type":18,"tag":215,"props":1651,"children":1652},{"class":217,"line":218},[1653],{"type":18,"tag":215,"props":1654,"children":1655},{},[1656],{"type":28,"value":1645},{"type":18,"tag":30,"props":1658,"children":1659},{},[1660],{"type":28,"value":1661},"执行结果如下：",{"type":18,"tag":205,"props":1663,"children":1665},{"code":1664},"[(2, 'xiaolan'), (3, 'xiaoliu')]\n",[1666],{"type":18,"tag":77,"props":1667,"children":1668},{"__ignoreMap":7},[1669],{"type":28,"value":1664},{"type":18,"tag":271,"props":1671,"children":1672},{},[1673],{"type":18,"tag":30,"props":1674,"children":1675},{},[1676],{"type":28,"value":1677},"说明：使用占位符的方式可以避免SQL注入的风险，推荐使用这种方式。",{"type":18,"tag":65,"props":1679,"children":1681},{"id":1680},"_3修改用户数据信息",[1682],{"type":28,"value":1683},"3）修改用户数据信息",{"type":18,"tag":30,"props":1685,"children":1686},{},[1687],{"type":28,"value":1688},"修改user表中的数据可以使用如下SQL语句：",{"type":18,"tag":205,"props":1690,"children":1692},{"code":1691,"language":905,"meta":7,"className":906,"style":7},"update  表名  set 字段名 = 字段值  where 查询条件\n",[1693],{"type":18,"tag":77,"props":1694,"children":1695},{"__ignoreMap":7},[1696],{"type":18,"tag":215,"props":1697,"children":1698},{"class":217,"line":218},[1699],{"type":18,"tag":215,"props":1700,"children":1701},{},[1702],{"type":28,"value":1691},{"type":18,"tag":30,"props":1704,"children":1705},{},[1706],{"type":28,"value":1707},"下面通过一个实例来学习一下如何修改表中数据。",{"type":18,"tag":30,"props":1709,"children":1710},{},[1711],{"type":18,"tag":36,"props":1712,"children":1713},{},[1714],{"type":28,"value":1715},"修改用户数据信息",{"type":18,"tag":30,"props":1717,"children":1718},{},[1719,1721,1727,1729,1735,1737,1742],{"type":28,"value":1720},"将SQLite数据库中user表ID为1的数据name字段值",{"type":18,"tag":77,"props":1722,"children":1724},{"className":1723},[],[1725],{"type":28,"value":1726},"xiaozhang",{"type":28,"value":1728},"修改为",{"type":18,"tag":77,"props":1730,"children":1732},{"className":1731},[],[1733],{"type":28,"value":1734},"Xiao",{"type":28,"value":1736},"，并使用",{"type":18,"tag":77,"props":1738,"children":1740},{"className":1739},[],[1741],{"type":28,"value":1309},{"type":28,"value":1743},"获取表中的所有数据。具体代码如下：",{"type":18,"tag":205,"props":1745,"children":1747},{"code":1746,"language":208,"meta":7,"className":209,"style":7},"import sqlite3\n\n# 连接到SQLite数据库，数据库文件是info.db\nconn = sqlite3.connect('info.db')\n\n# 创建一个Cursor:\ncursor = conn.cursor()\ncursor.execute('update user set name = ? where id = ?',('Xiao',1))\ncursor.execute('select * from user')\nresult = cursor.fetchall()\nprint(result)\n\n# 关闭游标\ncursor.close()\n\n# 提交事务\nconn.commit()\n\n# 关闭Connection:\nconn.close()\n",[1748],{"type":18,"tag":77,"props":1749,"children":1750},{"__ignoreMap":7},[1751,1758,1765,1773,1780,1787,1795,1802,1810,1817,1825,1833,1840,1847,1854,1861,1868,1875,1882,1890],{"type":18,"tag":215,"props":1752,"children":1753},{"class":217,"line":218},[1754],{"type":18,"tag":215,"props":1755,"children":1756},{},[1757],{"type":28,"value":711},{"type":18,"tag":215,"props":1759,"children":1760},{"class":217,"line":227},[1761],{"type":18,"tag":215,"props":1762,"children":1763},{"emptyLinePlaceholder":717},[1764],{"type":28,"value":720},{"type":18,"tag":215,"props":1766,"children":1767},{"class":217,"line":236},[1768],{"type":18,"tag":215,"props":1769,"children":1770},{},[1771],{"type":28,"value":1772},"# 连接到SQLite数据库，数据库文件是info.db\n",{"type":18,"tag":215,"props":1774,"children":1775},{"class":217,"line":245},[1776],{"type":18,"tag":215,"props":1777,"children":1778},{},[1779],{"type":28,"value":744},{"type":18,"tag":215,"props":1781,"children":1782},{"class":217,"line":254},[1783],{"type":18,"tag":215,"props":1784,"children":1785},{"emptyLinePlaceholder":717},[1786],{"type":28,"value":720},{"type":18,"tag":215,"props":1788,"children":1789},{"class":217,"line":263},[1790],{"type":18,"tag":215,"props":1791,"children":1792},{},[1793],{"type":28,"value":1794},"# 创建一个Cursor:\n",{"type":18,"tag":215,"props":1796,"children":1797},{"class":217,"line":754},[1798],{"type":18,"tag":215,"props":1799,"children":1800},{},[1801],{"type":28,"value":769},{"type":18,"tag":215,"props":1803,"children":1804},{"class":217,"line":763},[1805],{"type":18,"tag":215,"props":1806,"children":1807},{},[1808],{"type":28,"value":1809},"cursor.execute('update user set name = ? where id = ?',('Xiao',1))\n",{"type":18,"tag":215,"props":1811,"children":1812},{"class":217,"line":772},[1813],{"type":18,"tag":215,"props":1814,"children":1815},{},[1816],{"type":28,"value":1391},{"type":18,"tag":215,"props":1818,"children":1819},{"class":217,"line":780},[1820],{"type":18,"tag":215,"props":1821,"children":1822},{},[1823],{"type":28,"value":1824},"result = cursor.fetchall()\n",{"type":18,"tag":215,"props":1826,"children":1827},{"class":217,"line":789},[1828],{"type":18,"tag":215,"props":1829,"children":1830},{},[1831],{"type":28,"value":1832},"print(result)\n",{"type":18,"tag":215,"props":1834,"children":1835},{"class":217,"line":798},[1836],{"type":18,"tag":215,"props":1837,"children":1838},{"emptyLinePlaceholder":717},[1839],{"type":28,"value":720},{"type":18,"tag":215,"props":1841,"children":1842},{"class":217,"line":806},[1843],{"type":18,"tag":215,"props":1844,"children":1845},{},[1846],{"type":28,"value":812},{"type":18,"tag":215,"props":1848,"children":1849},{"class":217,"line":815},[1850],{"type":18,"tag":215,"props":1851,"children":1852},{},[1853],{"type":28,"value":821},{"type":18,"tag":215,"props":1855,"children":1856},{"class":217,"line":824},[1857],{"type":18,"tag":215,"props":1858,"children":1859},{"emptyLinePlaceholder":717},[1860],{"type":28,"value":720},{"type":18,"tag":215,"props":1862,"children":1863},{"class":217,"line":832},[1864],{"type":18,"tag":215,"props":1865,"children":1866},{},[1867],{"type":28,"value":1161},{"type":18,"tag":215,"props":1869,"children":1870},{"class":217,"line":841},[1871],{"type":18,"tag":215,"props":1872,"children":1873},{},[1874],{"type":28,"value":1170},{"type":18,"tag":215,"props":1876,"children":1877},{"class":217,"line":1147},[1878],{"type":18,"tag":215,"props":1879,"children":1880},{"emptyLinePlaceholder":717},[1881],{"type":28,"value":720},{"type":18,"tag":215,"props":1883,"children":1884},{"class":217,"line":1155},[1885],{"type":18,"tag":215,"props":1886,"children":1887},{},[1888],{"type":28,"value":1889},"# 关闭Connection:\n",{"type":18,"tag":215,"props":1891,"children":1892},{"class":217,"line":1164},[1893],{"type":18,"tag":215,"props":1894,"children":1895},{},[1896],{"type":28,"value":847},{"type":18,"tag":30,"props":1898,"children":1899},{},[1900],{"type":28,"value":1661},{"type":18,"tag":205,"props":1902,"children":1904},{"code":1903},"[(1, 'Xiao'), (2, 'xiaolan'), (3, 'xiaoliu')]\n",[1905],{"type":18,"tag":77,"props":1906,"children":1907},{"__ignoreMap":7},[1908],{"type":28,"value":1903},{"type":18,"tag":65,"props":1910,"children":1912},{"id":1911},"_4删除用户数据信息",[1913],{"type":28,"value":1914},"4）删除用户数据信息",{"type":18,"tag":30,"props":1916,"children":1917},{},[1918],{"type":28,"value":1919},"删除user表中的数据可以使用如下SQL语句：",{"type":18,"tag":205,"props":1921,"children":1923},{"code":1922,"language":905,"meta":7,"className":906,"style":7},"delete  from 表名  where 查询条件\n",[1924],{"type":18,"tag":77,"props":1925,"children":1926},{"__ignoreMap":7},[1927],{"type":18,"tag":215,"props":1928,"children":1929},{"class":217,"line":218},[1930],{"type":18,"tag":215,"props":1931,"children":1932},{},[1933],{"type":28,"value":1922},{"type":18,"tag":30,"props":1935,"children":1936},{},[1937],{"type":28,"value":1938},"下面通过一个实例来学习如何删除表中数据。",{"type":18,"tag":30,"props":1940,"children":1941},{},[1942],{"type":18,"tag":36,"props":1943,"children":1944},{},[1945],{"type":28,"value":1946},"删除用户数据信息",{"type":18,"tag":30,"props":1948,"children":1949},{},[1950,1952,1957],{"type":28,"value":1951},"将SQLite数据库中user表ID为1的数据删除，并使用",{"type":18,"tag":77,"props":1953,"children":1955},{"className":1954},[],[1956],{"type":28,"value":1309},{"type":28,"value":1958},"获取表中的所有数据，查看删除后的结果。具体代码如下：",{"type":18,"tag":205,"props":1960,"children":1962},{"code":1961,"language":208,"meta":7,"className":209,"style":7},"import sqlite3\n\n# 连接到SQLite数据库，数据库文件是info.db\nconn = sqlite3.connect('info.db')\n\n# 创建一个Cursor:\ncursor = conn.cursor()\ncursor.execute('delete from user where id = ?',(1,))\ncursor.execute('select * from user')\nresult = cursor.fetchall()\nprint(result)\n\n# 关闭游标\ncursor.close()\n\n# 提交事务\nconn.commit()\n\n# 关闭Connection:\nconn.close()\n",[1963],{"type":18,"tag":77,"props":1964,"children":1965},{"__ignoreMap":7},[1966,1973,1980,1987,1994,2001,2008,2015,2023,2030,2037,2044,2051,2058,2065,2072,2079,2086,2093,2100],{"type":18,"tag":215,"props":1967,"children":1968},{"class":217,"line":218},[1969],{"type":18,"tag":215,"props":1970,"children":1971},{},[1972],{"type":28,"value":711},{"type":18,"tag":215,"props":1974,"children":1975},{"class":217,"line":227},[1976],{"type":18,"tag":215,"props":1977,"children":1978},{"emptyLinePlaceholder":717},[1979],{"type":28,"value":720},{"type":18,"tag":215,"props":1981,"children":1982},{"class":217,"line":236},[1983],{"type":18,"tag":215,"props":1984,"children":1985},{},[1986],{"type":28,"value":1772},{"type":18,"tag":215,"props":1988,"children":1989},{"class":217,"line":245},[1990],{"type":18,"tag":215,"props":1991,"children":1992},{},[1993],{"type":28,"value":744},{"type":18,"tag":215,"props":1995,"children":1996},{"class":217,"line":254},[1997],{"type":18,"tag":215,"props":1998,"children":1999},{"emptyLinePlaceholder":717},[2000],{"type":28,"value":720},{"type":18,"tag":215,"props":2002,"children":2003},{"class":217,"line":263},[2004],{"type":18,"tag":215,"props":2005,"children":2006},{},[2007],{"type":28,"value":1794},{"type":18,"tag":215,"props":2009,"children":2010},{"class":217,"line":754},[2011],{"type":18,"tag":215,"props":2012,"children":2013},{},[2014],{"type":28,"value":769},{"type":18,"tag":215,"props":2016,"children":2017},{"class":217,"line":763},[2018],{"type":18,"tag":215,"props":2019,"children":2020},{},[2021],{"type":28,"value":2022},"cursor.execute('delete from user where id = ?',(1,))\n",{"type":18,"tag":215,"props":2024,"children":2025},{"class":217,"line":772},[2026],{"type":18,"tag":215,"props":2027,"children":2028},{},[2029],{"type":28,"value":1391},{"type":18,"tag":215,"props":2031,"children":2032},{"class":217,"line":780},[2033],{"type":18,"tag":215,"props":2034,"children":2035},{},[2036],{"type":28,"value":1824},{"type":18,"tag":215,"props":2038,"children":2039},{"class":217,"line":789},[2040],{"type":18,"tag":215,"props":2041,"children":2042},{},[2043],{"type":28,"value":1832},{"type":18,"tag":215,"props":2045,"children":2046},{"class":217,"line":798},[2047],{"type":18,"tag":215,"props":2048,"children":2049},{"emptyLinePlaceholder":717},[2050],{"type":28,"value":720},{"type":18,"tag":215,"props":2052,"children":2053},{"class":217,"line":806},[2054],{"type":18,"tag":215,"props":2055,"children":2056},{},[2057],{"type":28,"value":812},{"type":18,"tag":215,"props":2059,"children":2060},{"class":217,"line":815},[2061],{"type":18,"tag":215,"props":2062,"children":2063},{},[2064],{"type":28,"value":821},{"type":18,"tag":215,"props":2066,"children":2067},{"class":217,"line":824},[2068],{"type":18,"tag":215,"props":2069,"children":2070},{"emptyLinePlaceholder":717},[2071],{"type":28,"value":720},{"type":18,"tag":215,"props":2073,"children":2074},{"class":217,"line":832},[2075],{"type":18,"tag":215,"props":2076,"children":2077},{},[2078],{"type":28,"value":1161},{"type":18,"tag":215,"props":2080,"children":2081},{"class":217,"line":841},[2082],{"type":18,"tag":215,"props":2083,"children":2084},{},[2085],{"type":28,"value":1170},{"type":18,"tag":215,"props":2087,"children":2088},{"class":217,"line":1147},[2089],{"type":18,"tag":215,"props":2090,"children":2091},{"emptyLinePlaceholder":717},[2092],{"type":28,"value":720},{"type":18,"tag":215,"props":2094,"children":2095},{"class":217,"line":1155},[2096],{"type":18,"tag":215,"props":2097,"children":2098},{},[2099],{"type":28,"value":1889},{"type":18,"tag":215,"props":2101,"children":2102},{"class":217,"line":1164},[2103],{"type":18,"tag":215,"props":2104,"children":2105},{},[2106],{"type":28,"value":847},{"type":18,"tag":30,"props":2108,"children":2109},{},[2110],{"type":28,"value":2111},"执行上述代码后，user表中ID为1的数据将被删除。运行结果如下：",{"type":18,"tag":205,"props":2113,"children":2114},{"code":1664},[2115],{"type":18,"tag":77,"props":2116,"children":2117},{"__ignoreMap":7},[2118],{"type":28,"value":1664},{"type":18,"tag":23,"props":2120,"children":2122},{"id":2121},"使用mysql",[2123],{"type":28,"value":2124},"使用MySQL",{"type":18,"tag":51,"props":2126,"children":2128},{"id":2127},"安装pymysql",[2129],{"type":28,"value":2130},"安装PyMySQL",{"type":18,"tag":30,"props":2132,"children":2133},{},[2134],{"type":28,"value":2135},"由于MySQL服务器以独立的进程运行，并通过网络对外服务，所以，需要支持Python的MySQL驱动来连接到MySQL服务器。在Python中支持MySQL的数据库模块有很多，我们选择使用PyMySQL。",{"type":18,"tag":30,"props":2137,"children":2138},{},[2139],{"type":28,"value":2140},"PyMySQL的安装比较简单，在终端中执行如下命令：",{"type":18,"tag":205,"props":2142,"children":2146},{"code":2143,"language":2144,"meta":7,"className":2145,"style":7},"pip install pymysql\n","shell","language-shell shiki shiki-themes github-dark",[2147],{"type":18,"tag":77,"props":2148,"children":2149},{"__ignoreMap":7},[2150],{"type":18,"tag":215,"props":2151,"children":2152},{"class":217,"line":218},[2153,2159,2165],{"type":18,"tag":215,"props":2154,"children":2156},{"style":2155},"--shiki-default:#B392F0",[2157],{"type":28,"value":2158},"pip",{"type":18,"tag":215,"props":2160,"children":2162},{"style":2161},"--shiki-default:#9ECBFF",[2163],{"type":28,"value":2164}," install",{"type":18,"tag":215,"props":2166,"children":2167},{"style":2161},[2168],{"type":28,"value":2169}," pymysql\n",{"type":18,"tag":30,"props":2171,"children":2172},{},[2173],{"type":28,"value":2174},"运行结果如图12所示。",{"type":18,"tag":30,"props":2176,"children":2177},{},[2178],{"type":18,"tag":687,"props":2179,"children":2182},{"alt":2180,"src":2181},"Pasted image 20250721105513","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250721105513.png",[],{"type":18,"tag":51,"props":2184,"children":2186},{"id":2185},"连接数据库",[2187],{"type":28,"value":2185},{"type":18,"tag":30,"props":2189,"children":2190},{},[2191],{"type":28,"value":2192},"使用数据库的第一步是连接数据库。接下来使用PyMySQL连接数据库。由于PyMySQL也遵循Python Database API 2.0规范，所以操作MySQL数据库的方式与SQLite相似。我们可以通过类比的方式来学习。",{"type":18,"tag":30,"props":2194,"children":2195},{},[2196],{"type":18,"tag":36,"props":2197,"children":2198},{},[2199],{"type":28,"value":2200},"使用PyMySQL连接数据库",{"type":18,"tag":30,"props":2202,"children":2203},{},[2204,2206,2212],{"type":28,"value":2205},"前面我们已经创建了一个MySQL连接",{"type":18,"tag":77,"props":2207,"children":2209},{"className":2208},[],[2210],{"type":28,"value":2211},"studyPython",{"type":28,"value":2213},"，并且在安装数据库时设置了数据库的用户名“root”和密码“123456”。下面通过connect()方法连接MySQL数据库info，具体代码如下：",{"type":18,"tag":205,"props":2215,"children":2217},{"code":2216,"language":208,"meta":7,"className":209,"style":7},"import pymysql\n\n# 打开数据库连接，参数1：主机名或IP；参数2：用户名；参数3：密码；参数4：数据库名称\ndb = pymysql.connect(  \n    host=\"localhost\",  \n    user=\"root\",  \n    password=\"123456\",  \n    db=\"info\" \n)\n\n# 使用cursor()方法创建一个游标对象cursor\ncursor = db.cursor()\n\n# 使用execute()方法执行SQL查询\ncursor.execute(\"SELECT VERSION()\")\n\n# 使用fetchone()方法获取单条数据\ndata = cursor.fetchone()\nprint (\"Database version : %s \" % data)\n\n# 关闭数据库连接\ndb.close()\n",[2218],{"type":18,"tag":77,"props":2219,"children":2220},{"__ignoreMap":7},[2221,2229,2236,2244,2252,2260,2268,2276,2284,2292,2299,2307,2315,2322,2330,2338,2345,2353,2361,2369,2376,2384],{"type":18,"tag":215,"props":2222,"children":2223},{"class":217,"line":218},[2224],{"type":18,"tag":215,"props":2225,"children":2226},{},[2227],{"type":28,"value":2228},"import pymysql\n",{"type":18,"tag":215,"props":2230,"children":2231},{"class":217,"line":227},[2232],{"type":18,"tag":215,"props":2233,"children":2234},{"emptyLinePlaceholder":717},[2235],{"type":28,"value":720},{"type":18,"tag":215,"props":2237,"children":2238},{"class":217,"line":236},[2239],{"type":18,"tag":215,"props":2240,"children":2241},{},[2242],{"type":28,"value":2243},"# 打开数据库连接，参数1：主机名或IP；参数2：用户名；参数3：密码；参数4：数据库名称\n",{"type":18,"tag":215,"props":2245,"children":2246},{"class":217,"line":245},[2247],{"type":18,"tag":215,"props":2248,"children":2249},{},[2250],{"type":28,"value":2251},"db = pymysql.connect(  \n",{"type":18,"tag":215,"props":2253,"children":2254},{"class":217,"line":254},[2255],{"type":18,"tag":215,"props":2256,"children":2257},{},[2258],{"type":28,"value":2259},"    host=\"localhost\",  \n",{"type":18,"tag":215,"props":2261,"children":2262},{"class":217,"line":263},[2263],{"type":18,"tag":215,"props":2264,"children":2265},{},[2266],{"type":28,"value":2267},"    user=\"root\",  \n",{"type":18,"tag":215,"props":2269,"children":2270},{"class":217,"line":754},[2271],{"type":18,"tag":215,"props":2272,"children":2273},{},[2274],{"type":28,"value":2275},"    password=\"123456\",  \n",{"type":18,"tag":215,"props":2277,"children":2278},{"class":217,"line":763},[2279],{"type":18,"tag":215,"props":2280,"children":2281},{},[2282],{"type":28,"value":2283},"    db=\"info\" \n",{"type":18,"tag":215,"props":2285,"children":2286},{"class":217,"line":772},[2287],{"type":18,"tag":215,"props":2288,"children":2289},{},[2290],{"type":28,"value":2291},")\n",{"type":18,"tag":215,"props":2293,"children":2294},{"class":217,"line":780},[2295],{"type":18,"tag":215,"props":2296,"children":2297},{"emptyLinePlaceholder":717},[2298],{"type":28,"value":720},{"type":18,"tag":215,"props":2300,"children":2301},{"class":217,"line":789},[2302],{"type":18,"tag":215,"props":2303,"children":2304},{},[2305],{"type":28,"value":2306},"# 使用cursor()方法创建一个游标对象cursor\n",{"type":18,"tag":215,"props":2308,"children":2309},{"class":217,"line":798},[2310],{"type":18,"tag":215,"props":2311,"children":2312},{},[2313],{"type":28,"value":2314},"cursor = db.cursor()\n",{"type":18,"tag":215,"props":2316,"children":2317},{"class":217,"line":806},[2318],{"type":18,"tag":215,"props":2319,"children":2320},{"emptyLinePlaceholder":717},[2321],{"type":28,"value":720},{"type":18,"tag":215,"props":2323,"children":2324},{"class":217,"line":815},[2325],{"type":18,"tag":215,"props":2326,"children":2327},{},[2328],{"type":28,"value":2329},"# 使用execute()方法执行SQL查询\n",{"type":18,"tag":215,"props":2331,"children":2332},{"class":217,"line":824},[2333],{"type":18,"tag":215,"props":2334,"children":2335},{},[2336],{"type":28,"value":2337},"cursor.execute(\"SELECT VERSION()\")\n",{"type":18,"tag":215,"props":2339,"children":2340},{"class":217,"line":832},[2341],{"type":18,"tag":215,"props":2342,"children":2343},{"emptyLinePlaceholder":717},[2344],{"type":28,"value":720},{"type":18,"tag":215,"props":2346,"children":2347},{"class":217,"line":841},[2348],{"type":18,"tag":215,"props":2349,"children":2350},{},[2351],{"type":28,"value":2352},"# 使用fetchone()方法获取单条数据\n",{"type":18,"tag":215,"props":2354,"children":2355},{"class":217,"line":1147},[2356],{"type":18,"tag":215,"props":2357,"children":2358},{},[2359],{"type":28,"value":2360},"data = cursor.fetchone()\n",{"type":18,"tag":215,"props":2362,"children":2363},{"class":217,"line":1155},[2364],{"type":18,"tag":215,"props":2365,"children":2366},{},[2367],{"type":28,"value":2368},"print (\"Database version : %s \" % data)\n",{"type":18,"tag":215,"props":2370,"children":2371},{"class":217,"line":1164},[2372],{"type":18,"tag":215,"props":2373,"children":2374},{"emptyLinePlaceholder":717},[2375],{"type":28,"value":720},{"type":18,"tag":215,"props":2377,"children":2378},{"class":217,"line":1173},[2379],{"type":18,"tag":215,"props":2380,"children":2381},{},[2382],{"type":28,"value":2383},"# 关闭数据库连接\n",{"type":18,"tag":215,"props":2385,"children":2386},{"class":217,"line":1181},[2387],{"type":18,"tag":215,"props":2388,"children":2389},{},[2390],{"type":28,"value":2391},"db.close()\n",{"type":18,"tag":30,"props":2393,"children":2394},{},[2395,2397,2402,2404,2410,2412,2417,2419,2424],{"type":28,"value":2396},"上述代码中，首先使用connect()方法连接数据库，然后使用",{"type":18,"tag":77,"props":2398,"children":2400},{"className":2399},[],[2401],{"type":28,"value":384},{"type":28,"value":2403},"方法创建游标，接着使用",{"type":18,"tag":77,"props":2405,"children":2407},{"className":2406},[],[2408],{"type":28,"value":2409},"excute()",{"type":28,"value":2411},"方法执行SQL语句查看MySQL数据库版本，然后使用",{"type":18,"tag":77,"props":2413,"children":2415},{"className":2414},[],[2416],{"type":28,"value":578},{"type":28,"value":2418},"方法获取数据，最后使用",{"type":18,"tag":77,"props":2420,"children":2422},{"className":2421},[],[2423],{"type":28,"value":345},{"type":28,"value":2425},"方法关闭数据库连接。执行结果如下：",{"type":18,"tag":205,"props":2427,"children":2429},{"code":2428},"Database version : 8.0.41 \n",[2430],{"type":18,"tag":77,"props":2431,"children":2432},{"__ignoreMap":7},[2433],{"type":28,"value":2428},{"type":18,"tag":51,"props":2435,"children":2437},{"id":2436},"创建数据表",[2438],{"type":28,"value":2436},{"type":18,"tag":30,"props":2440,"children":2441},{},[2442,2444,2450],{"type":28,"value":2443},"数据库连接成功以后，我们就可以为数据库创建数据表了。下面通过一个实例，使用",{"type":18,"tag":77,"props":2445,"children":2447},{"className":2446},[],[2448],{"type":28,"value":2449},"execute()",{"type":28,"value":2451},"方法来为数据库创建表books图书表。",{"type":18,"tag":30,"props":2453,"children":2454},{},[2455],{"type":18,"tag":36,"props":2456,"children":2457},{},[2458],{"type":28,"value":2459},"创建books图书表",{"type":18,"tag":30,"props":2461,"children":2462},{},[2463],{"type":28,"value":2464},"books表包含id（主键）、name（图书名称），category（图书分类），price（图书价格）和publish_time（出版时间）5个字段。创建books表的SQL语句如下：",{"type":18,"tag":205,"props":2466,"children":2468},{"code":2467,"language":905,"meta":7,"className":906,"style":7},"CREATE TABLE books (\n  id int(8) NOT NULL AUTO_INCREMENT,\n  name varchar(50) NOT NULL,\n  category varchar(50) NOT NULL,\n  price decimal(10,2) DEFAULT NULL,\n  publish_time date DEFAULT NULL,\n  PRIMARY KEY (id)\n) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;\n",[2469],{"type":18,"tag":77,"props":2470,"children":2471},{"__ignoreMap":7},[2472,2480,2488,2496,2504,2512,2520,2528],{"type":18,"tag":215,"props":2473,"children":2474},{"class":217,"line":218},[2475],{"type":18,"tag":215,"props":2476,"children":2477},{},[2478],{"type":28,"value":2479},"CREATE TABLE books (\n",{"type":18,"tag":215,"props":2481,"children":2482},{"class":217,"line":227},[2483],{"type":18,"tag":215,"props":2484,"children":2485},{},[2486],{"type":28,"value":2487},"  id int(8) NOT NULL AUTO_INCREMENT,\n",{"type":18,"tag":215,"props":2489,"children":2490},{"class":217,"line":236},[2491],{"type":18,"tag":215,"props":2492,"children":2493},{},[2494],{"type":28,"value":2495},"  name varchar(50) NOT NULL,\n",{"type":18,"tag":215,"props":2497,"children":2498},{"class":217,"line":245},[2499],{"type":18,"tag":215,"props":2500,"children":2501},{},[2502],{"type":28,"value":2503},"  category varchar(50) NOT NULL,\n",{"type":18,"tag":215,"props":2505,"children":2506},{"class":217,"line":254},[2507],{"type":18,"tag":215,"props":2508,"children":2509},{},[2510],{"type":28,"value":2511},"  price decimal(10,2) DEFAULT NULL,\n",{"type":18,"tag":215,"props":2513,"children":2514},{"class":217,"line":263},[2515],{"type":18,"tag":215,"props":2516,"children":2517},{},[2518],{"type":28,"value":2519},"  publish_time date DEFAULT NULL,\n",{"type":18,"tag":215,"props":2521,"children":2522},{"class":217,"line":754},[2523],{"type":18,"tag":215,"props":2524,"children":2525},{},[2526],{"type":28,"value":2527},"  PRIMARY KEY (id)\n",{"type":18,"tag":215,"props":2529,"children":2530},{"class":217,"line":763},[2531],{"type":18,"tag":215,"props":2532,"children":2533},{},[2534],{"type":28,"value":2535},") ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;\n",{"type":18,"tag":30,"props":2537,"children":2538},{},[2539],{"type":28,"value":2540},"在创建数据表前，使用如下语句：",{"type":18,"tag":205,"props":2542,"children":2544},{"code":2543,"language":905,"meta":7,"className":906,"style":7},"DROP TABLE IF EXISTS `books`;\n",[2545],{"type":18,"tag":77,"props":2546,"children":2547},{"__ignoreMap":7},[2548],{"type":18,"tag":215,"props":2549,"children":2550},{"class":217,"line":218},[2551],{"type":18,"tag":215,"props":2552,"children":2553},{},[2554],{"type":28,"value":2543},{"type":18,"tag":30,"props":2556,"children":2557},{},[2558],{"type":28,"value":2559},"如果info数据库中已经存在books，那么先删除books，然后再创建books数据表。具体代码如下：",{"type":18,"tag":205,"props":2561,"children":2563},{"code":2562,"language":208,"meta":7,"className":209,"style":7},"import pymysql\n\n# 打开数据库连接\ndb = pymysql.connect(  \n    host=\"localhost\",  \n    user=\"root\",  \n    password=\"123456\",  \n    db=\"info\" \n)\n\n# 使用cursor()方法创建一个游标对象cursor\ncursor = db.cursor()\n\n# 使用 execute()方法执行SQL，如果表存在则删除\ncursor.execute(\"DROP TABLE IF EXISTS books\")\n\n# 使用预处理语句创建表\nsql = \"\"\"\nCREATE TABLE books (\n  id int(8) NOT NULL AUTO_INCREMENT,\n  name varchar(50) NOT NULL,\n  category varchar(50) NOT NULL,\n  price decimal(10,2) DEFAULT NULL,\n  publish_time date DEFAULT NULL,\n  PRIMARY KEY (id)\n) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;\n\"\"\"\n\n# 执行SQL语句\ncursor.execute(sql)\n\n# 关闭数据库连接\ndb.close()\n",[2564],{"type":18,"tag":77,"props":2565,"children":2566},{"__ignoreMap":7},[2567,2574,2581,2589,2596,2603,2610,2617,2624,2631,2638,2645,2652,2659,2667,2675,2682,2690,2698,2705,2712,2719,2727,2734,2742,2750,2758,2767,2775,2784,2793,2801,2809],{"type":18,"tag":215,"props":2568,"children":2569},{"class":217,"line":218},[2570],{"type":18,"tag":215,"props":2571,"children":2572},{},[2573],{"type":28,"value":2228},{"type":18,"tag":215,"props":2575,"children":2576},{"class":217,"line":227},[2577],{"type":18,"tag":215,"props":2578,"children":2579},{"emptyLinePlaceholder":717},[2580],{"type":28,"value":720},{"type":18,"tag":215,"props":2582,"children":2583},{"class":217,"line":236},[2584],{"type":18,"tag":215,"props":2585,"children":2586},{},[2587],{"type":28,"value":2588},"# 打开数据库连接\n",{"type":18,"tag":215,"props":2590,"children":2591},{"class":217,"line":245},[2592],{"type":18,"tag":215,"props":2593,"children":2594},{},[2595],{"type":28,"value":2251},{"type":18,"tag":215,"props":2597,"children":2598},{"class":217,"line":254},[2599],{"type":18,"tag":215,"props":2600,"children":2601},{},[2602],{"type":28,"value":2259},{"type":18,"tag":215,"props":2604,"children":2605},{"class":217,"line":263},[2606],{"type":18,"tag":215,"props":2607,"children":2608},{},[2609],{"type":28,"value":2267},{"type":18,"tag":215,"props":2611,"children":2612},{"class":217,"line":754},[2613],{"type":18,"tag":215,"props":2614,"children":2615},{},[2616],{"type":28,"value":2275},{"type":18,"tag":215,"props":2618,"children":2619},{"class":217,"line":763},[2620],{"type":18,"tag":215,"props":2621,"children":2622},{},[2623],{"type":28,"value":2283},{"type":18,"tag":215,"props":2625,"children":2626},{"class":217,"line":772},[2627],{"type":18,"tag":215,"props":2628,"children":2629},{},[2630],{"type":28,"value":2291},{"type":18,"tag":215,"props":2632,"children":2633},{"class":217,"line":780},[2634],{"type":18,"tag":215,"props":2635,"children":2636},{"emptyLinePlaceholder":717},[2637],{"type":28,"value":720},{"type":18,"tag":215,"props":2639,"children":2640},{"class":217,"line":789},[2641],{"type":18,"tag":215,"props":2642,"children":2643},{},[2644],{"type":28,"value":2306},{"type":18,"tag":215,"props":2646,"children":2647},{"class":217,"line":798},[2648],{"type":18,"tag":215,"props":2649,"children":2650},{},[2651],{"type":28,"value":2314},{"type":18,"tag":215,"props":2653,"children":2654},{"class":217,"line":806},[2655],{"type":18,"tag":215,"props":2656,"children":2657},{"emptyLinePlaceholder":717},[2658],{"type":28,"value":720},{"type":18,"tag":215,"props":2660,"children":2661},{"class":217,"line":815},[2662],{"type":18,"tag":215,"props":2663,"children":2664},{},[2665],{"type":28,"value":2666},"# 使用 execute()方法执行SQL，如果表存在则删除\n",{"type":18,"tag":215,"props":2668,"children":2669},{"class":217,"line":824},[2670],{"type":18,"tag":215,"props":2671,"children":2672},{},[2673],{"type":28,"value":2674},"cursor.execute(\"DROP TABLE IF EXISTS books\")\n",{"type":18,"tag":215,"props":2676,"children":2677},{"class":217,"line":832},[2678],{"type":18,"tag":215,"props":2679,"children":2680},{"emptyLinePlaceholder":717},[2681],{"type":28,"value":720},{"type":18,"tag":215,"props":2683,"children":2684},{"class":217,"line":841},[2685],{"type":18,"tag":215,"props":2686,"children":2687},{},[2688],{"type":28,"value":2689},"# 使用预处理语句创建表\n",{"type":18,"tag":215,"props":2691,"children":2692},{"class":217,"line":1147},[2693],{"type":18,"tag":215,"props":2694,"children":2695},{},[2696],{"type":28,"value":2697},"sql = \"\"\"\n",{"type":18,"tag":215,"props":2699,"children":2700},{"class":217,"line":1155},[2701],{"type":18,"tag":215,"props":2702,"children":2703},{},[2704],{"type":28,"value":2479},{"type":18,"tag":215,"props":2706,"children":2707},{"class":217,"line":1164},[2708],{"type":18,"tag":215,"props":2709,"children":2710},{},[2711],{"type":28,"value":2487},{"type":18,"tag":215,"props":2713,"children":2714},{"class":217,"line":1173},[2715],{"type":18,"tag":215,"props":2716,"children":2717},{},[2718],{"type":28,"value":2495},{"type":18,"tag":215,"props":2720,"children":2721},{"class":217,"line":1181},[2722],{"type":18,"tag":215,"props":2723,"children":2724},{},[2725],{"type":28,"value":2726},"  category varchar(50) NOT NULL,\n",{"type":18,"tag":215,"props":2728,"children":2729},{"class":217,"line":1189},[2730],{"type":18,"tag":215,"props":2731,"children":2732},{},[2733],{"type":28,"value":2511},{"type":18,"tag":215,"props":2735,"children":2737},{"class":217,"line":2736},24,[2738],{"type":18,"tag":215,"props":2739,"children":2740},{},[2741],{"type":28,"value":2519},{"type":18,"tag":215,"props":2743,"children":2745},{"class":217,"line":2744},25,[2746],{"type":18,"tag":215,"props":2747,"children":2748},{},[2749],{"type":28,"value":2527},{"type":18,"tag":215,"props":2751,"children":2753},{"class":217,"line":2752},26,[2754],{"type":18,"tag":215,"props":2755,"children":2756},{},[2757],{"type":28,"value":2535},{"type":18,"tag":215,"props":2759,"children":2761},{"class":217,"line":2760},27,[2762],{"type":18,"tag":215,"props":2763,"children":2764},{},[2765],{"type":28,"value":2766},"\"\"\"\n",{"type":18,"tag":215,"props":2768,"children":2770},{"class":217,"line":2769},28,[2771],{"type":18,"tag":215,"props":2772,"children":2773},{"emptyLinePlaceholder":717},[2774],{"type":28,"value":720},{"type":18,"tag":215,"props":2776,"children":2778},{"class":217,"line":2777},29,[2779],{"type":18,"tag":215,"props":2780,"children":2781},{},[2782],{"type":28,"value":2783},"# 执行SQL语句\n",{"type":18,"tag":215,"props":2785,"children":2787},{"class":217,"line":2786},30,[2788],{"type":18,"tag":215,"props":2789,"children":2790},{},[2791],{"type":28,"value":2792},"cursor.execute(sql)\n",{"type":18,"tag":215,"props":2794,"children":2796},{"class":217,"line":2795},31,[2797],{"type":18,"tag":215,"props":2798,"children":2799},{"emptyLinePlaceholder":717},[2800],{"type":28,"value":720},{"type":18,"tag":215,"props":2802,"children":2804},{"class":217,"line":2803},32,[2805],{"type":18,"tag":215,"props":2806,"children":2807},{},[2808],{"type":28,"value":2383},{"type":18,"tag":215,"props":2810,"children":2812},{"class":217,"line":2811},33,[2813],{"type":18,"tag":215,"props":2814,"children":2815},{},[2816],{"type":28,"value":2391},{"type":18,"tag":30,"props":2818,"children":2819},{},[2820],{"type":28,"value":2821},"运行上述代码后，info数据库下就已经创建了一个books表。打开DataGrip（如果已经打开，则进行刷新），发现info数据库下多了一个books表，双击books，效果如图所示。",{"type":18,"tag":30,"props":2823,"children":2824},{},[2825],{"type":18,"tag":687,"props":2826,"children":2829},{"alt":2827,"src":2828},"Pasted image 20250721131222","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250721131222.png",[],{"type":18,"tag":51,"props":2831,"children":2833},{"id":2832},"操作mysql数据表",[2834],{"type":28,"value":2835},"操作MySQL数据表",{"type":18,"tag":30,"props":2837,"children":2838},{},[2839],{"type":28,"value":2840},"MySQL数据表的操作主要包括数据的增删改查，与操作SQLite类似，这里我们通过一个实例讲解如何向books表中新增数据，至于修改、查找和删除数据则不再赘述。",{"type":18,"tag":30,"props":2842,"children":2843},{},[2844],{"type":18,"tag":36,"props":2845,"children":2846},{},[2847],{"type":28,"value":2848},"向books图书表添加图书数据",{"type":18,"tag":30,"props":2850,"children":2851},{},[2852,2854,2859,2861,2867,2869,2874],{"type":28,"value":2853},"在向books图书表中插入图书数据时，可以使用",{"type":18,"tag":77,"props":2855,"children":2857},{"className":2856},[],[2858],{"type":28,"value":2409},{"type":28,"value":2860},"方法添加一条记录，也可以使用",{"type":18,"tag":77,"props":2862,"children":2864},{"className":2863},[],[2865],{"type":28,"value":2866},"executemany()",{"type":28,"value":2868},"方法批量添加多条记录，",{"type":18,"tag":77,"props":2870,"children":2872},{"className":2871},[],[2873],{"type":28,"value":2866},{"type":28,"value":2875},"方法的语法格式如下：",{"type":18,"tag":205,"props":2877,"children":2879},{"code":2878,"language":208,"meta":7,"className":209,"style":7},"executemany(operation, seq_of_params)\n",[2880],{"type":18,"tag":77,"props":2881,"children":2882},{"__ignoreMap":7},[2883],{"type":18,"tag":215,"props":2884,"children":2885},{"class":217,"line":218},[2886],{"type":18,"tag":215,"props":2887,"children":2888},{},[2889],{"type":28,"value":2878},{"type":18,"tag":401,"props":2891,"children":2892},{},[2893,2904],{"type":18,"tag":405,"props":2894,"children":2895},{},[2896,2902],{"type":18,"tag":77,"props":2897,"children":2899},{"className":2898},[],[2900],{"type":28,"value":2901},"operation",{"type":28,"value":2903},"：操作的SQL语句。",{"type":18,"tag":405,"props":2905,"children":2906},{},[2907,2913],{"type":18,"tag":77,"props":2908,"children":2910},{"className":2909},[],[2911],{"type":28,"value":2912},"seq_of_params",{"type":28,"value":2914},"：参数序列。",{"type":18,"tag":30,"props":2916,"children":2917},{},[2918,2923],{"type":18,"tag":77,"props":2919,"children":2921},{"className":2920},[],[2922],{"type":28,"value":2866},{"type":28,"value":2924},"方法批量添加多条记录的具体代码如下：",{"type":18,"tag":205,"props":2926,"children":2928},{"code":2927,"language":208,"meta":7,"className":209,"style":7},"import pymysql\n\n# 打开数据库连接\ndb = pymysql.connect(  \n    host=\"localhost\",  \n    user=\"root\",  \n    password=\"123456\",  \n    db=\"info\" \n)\n\n# 使用cursor()方法获取操作游标\ncursor = db.cursor()\n\n# 数据列表\ndata = [(\"零基础学Python\",'Python','79.80','2018-5-20'),\n        (\"Python从入门到精通\",'Python','69.80','2018-6-18'),\n        (\"零基础学PHP\",'PHP','69.80','2017-5-21'),\n        (\"PHP项目开发实战入门\",'PHP','79.80','2016-5-21'),\n        (\"零基础学Java\",'Java','69.80','2017-5-21'),\n        ]\ntry:\n    # 执行sql语句，插入多条数据\n    cursor.executemany(\"insert into books(name, category, price, publish_time) values (%s,%s,%s,%s)\", data)\n    \n    # 提交数据\n    db.commit()\n\nexcept:\n\n    # 发生错误时回滚\n    db.rollback()\n\n# 关闭数据库连接\ndb.close()\n",[2929],{"type":18,"tag":77,"props":2930,"children":2931},{"__ignoreMap":7},[2932,2939,2946,2953,2960,2967,2974,2981,2988,2995,3002,3010,3017,3024,3032,3040,3048,3056,3064,3072,3080,3088,3096,3104,3112,3120,3128,3135,3143,3150,3158,3166,3173,3180],{"type":18,"tag":215,"props":2933,"children":2934},{"class":217,"line":218},[2935],{"type":18,"tag":215,"props":2936,"children":2937},{},[2938],{"type":28,"value":2228},{"type":18,"tag":215,"props":2940,"children":2941},{"class":217,"line":227},[2942],{"type":18,"tag":215,"props":2943,"children":2944},{"emptyLinePlaceholder":717},[2945],{"type":28,"value":720},{"type":18,"tag":215,"props":2947,"children":2948},{"class":217,"line":236},[2949],{"type":18,"tag":215,"props":2950,"children":2951},{},[2952],{"type":28,"value":2588},{"type":18,"tag":215,"props":2954,"children":2955},{"class":217,"line":245},[2956],{"type":18,"tag":215,"props":2957,"children":2958},{},[2959],{"type":28,"value":2251},{"type":18,"tag":215,"props":2961,"children":2962},{"class":217,"line":254},[2963],{"type":18,"tag":215,"props":2964,"children":2965},{},[2966],{"type":28,"value":2259},{"type":18,"tag":215,"props":2968,"children":2969},{"class":217,"line":263},[2970],{"type":18,"tag":215,"props":2971,"children":2972},{},[2973],{"type":28,"value":2267},{"type":18,"tag":215,"props":2975,"children":2976},{"class":217,"line":754},[2977],{"type":18,"tag":215,"props":2978,"children":2979},{},[2980],{"type":28,"value":2275},{"type":18,"tag":215,"props":2982,"children":2983},{"class":217,"line":763},[2984],{"type":18,"tag":215,"props":2985,"children":2986},{},[2987],{"type":28,"value":2283},{"type":18,"tag":215,"props":2989,"children":2990},{"class":217,"line":772},[2991],{"type":18,"tag":215,"props":2992,"children":2993},{},[2994],{"type":28,"value":2291},{"type":18,"tag":215,"props":2996,"children":2997},{"class":217,"line":780},[2998],{"type":18,"tag":215,"props":2999,"children":3000},{"emptyLinePlaceholder":717},[3001],{"type":28,"value":720},{"type":18,"tag":215,"props":3003,"children":3004},{"class":217,"line":789},[3005],{"type":18,"tag":215,"props":3006,"children":3007},{},[3008],{"type":28,"value":3009},"# 使用cursor()方法获取操作游标\n",{"type":18,"tag":215,"props":3011,"children":3012},{"class":217,"line":798},[3013],{"type":18,"tag":215,"props":3014,"children":3015},{},[3016],{"type":28,"value":2314},{"type":18,"tag":215,"props":3018,"children":3019},{"class":217,"line":806},[3020],{"type":18,"tag":215,"props":3021,"children":3022},{"emptyLinePlaceholder":717},[3023],{"type":28,"value":720},{"type":18,"tag":215,"props":3025,"children":3026},{"class":217,"line":815},[3027],{"type":18,"tag":215,"props":3028,"children":3029},{},[3030],{"type":28,"value":3031},"# 数据列表\n",{"type":18,"tag":215,"props":3033,"children":3034},{"class":217,"line":824},[3035],{"type":18,"tag":215,"props":3036,"children":3037},{},[3038],{"type":28,"value":3039},"data = [(\"零基础学Python\",'Python','79.80','2018-5-20'),\n",{"type":18,"tag":215,"props":3041,"children":3042},{"class":217,"line":832},[3043],{"type":18,"tag":215,"props":3044,"children":3045},{},[3046],{"type":28,"value":3047},"        (\"Python从入门到精通\",'Python','69.80','2018-6-18'),\n",{"type":18,"tag":215,"props":3049,"children":3050},{"class":217,"line":841},[3051],{"type":18,"tag":215,"props":3052,"children":3053},{},[3054],{"type":28,"value":3055},"        (\"零基础学PHP\",'PHP','69.80','2017-5-21'),\n",{"type":18,"tag":215,"props":3057,"children":3058},{"class":217,"line":1147},[3059],{"type":18,"tag":215,"props":3060,"children":3061},{},[3062],{"type":28,"value":3063},"        (\"PHP项目开发实战入门\",'PHP','79.80','2016-5-21'),\n",{"type":18,"tag":215,"props":3065,"children":3066},{"class":217,"line":1155},[3067],{"type":18,"tag":215,"props":3068,"children":3069},{},[3070],{"type":28,"value":3071},"        (\"零基础学Java\",'Java','69.80','2017-5-21'),\n",{"type":18,"tag":215,"props":3073,"children":3074},{"class":217,"line":1164},[3075],{"type":18,"tag":215,"props":3076,"children":3077},{},[3078],{"type":28,"value":3079},"        ]\n",{"type":18,"tag":215,"props":3081,"children":3082},{"class":217,"line":1173},[3083],{"type":18,"tag":215,"props":3084,"children":3085},{},[3086],{"type":28,"value":3087},"try:\n",{"type":18,"tag":215,"props":3089,"children":3090},{"class":217,"line":1181},[3091],{"type":18,"tag":215,"props":3092,"children":3093},{},[3094],{"type":28,"value":3095},"    # 执行sql语句，插入多条数据\n",{"type":18,"tag":215,"props":3097,"children":3098},{"class":217,"line":1189},[3099],{"type":18,"tag":215,"props":3100,"children":3101},{},[3102],{"type":28,"value":3103},"    cursor.executemany(\"insert into books(name, category, price, publish_time) values (%s,%s,%s,%s)\", data)\n",{"type":18,"tag":215,"props":3105,"children":3106},{"class":217,"line":2736},[3107],{"type":18,"tag":215,"props":3108,"children":3109},{},[3110],{"type":28,"value":3111},"    \n",{"type":18,"tag":215,"props":3113,"children":3114},{"class":217,"line":2744},[3115],{"type":18,"tag":215,"props":3116,"children":3117},{},[3118],{"type":28,"value":3119},"    # 提交数据\n",{"type":18,"tag":215,"props":3121,"children":3122},{"class":217,"line":2752},[3123],{"type":18,"tag":215,"props":3124,"children":3125},{},[3126],{"type":28,"value":3127},"    db.commit()\n",{"type":18,"tag":215,"props":3129,"children":3130},{"class":217,"line":2760},[3131],{"type":18,"tag":215,"props":3132,"children":3133},{"emptyLinePlaceholder":717},[3134],{"type":28,"value":720},{"type":18,"tag":215,"props":3136,"children":3137},{"class":217,"line":2769},[3138],{"type":18,"tag":215,"props":3139,"children":3140},{},[3141],{"type":28,"value":3142},"except:\n",{"type":18,"tag":215,"props":3144,"children":3145},{"class":217,"line":2777},[3146],{"type":18,"tag":215,"props":3147,"children":3148},{"emptyLinePlaceholder":717},[3149],{"type":28,"value":720},{"type":18,"tag":215,"props":3151,"children":3152},{"class":217,"line":2786},[3153],{"type":18,"tag":215,"props":3154,"children":3155},{},[3156],{"type":28,"value":3157},"    # 发生错误时回滚\n",{"type":18,"tag":215,"props":3159,"children":3160},{"class":217,"line":2795},[3161],{"type":18,"tag":215,"props":3162,"children":3163},{},[3164],{"type":28,"value":3165},"    db.rollback()\n",{"type":18,"tag":215,"props":3167,"children":3168},{"class":217,"line":2803},[3169],{"type":18,"tag":215,"props":3170,"children":3171},{"emptyLinePlaceholder":717},[3172],{"type":28,"value":720},{"type":18,"tag":215,"props":3174,"children":3175},{"class":217,"line":2811},[3176],{"type":18,"tag":215,"props":3177,"children":3178},{},[3179],{"type":28,"value":2383},{"type":18,"tag":215,"props":3181,"children":3183},{"class":217,"line":3182},34,[3184],{"type":18,"tag":215,"props":3185,"children":3186},{},[3187],{"type":28,"value":2391},{"type":18,"tag":30,"props":3189,"children":3190},{},[3191],{"type":28,"value":3192},"注意：",{"type":18,"tag":30,"props":3194,"children":3195},{},[3196,3198,3203,3205,3211,3213,3219,3221,3227],{"type":28,"value":3197},"（1）使用",{"type":18,"tag":77,"props":3199,"children":3201},{"className":3200},[],[3202],{"type":28,"value":82},{"type":28,"value":3204},"方法连接数据库时，额外设置字符集",{"type":18,"tag":77,"props":3206,"children":3208},{"className":3207},[],[3209],{"type":28,"value":3210},"charset=utf-8",{"type":28,"value":3212},"，可以防止插入中文时出错。\n（2）在使用",{"type":18,"tag":77,"props":3214,"children":3216},{"className":3215},[],[3217],{"type":28,"value":3218},"insert",{"type":28,"value":3220},"语句插入数据时，使用",{"type":18,"tag":77,"props":3222,"children":3224},{"className":3223},[],[3225],{"type":28,"value":3226},"%s",{"type":28,"value":3228},"作为占位符，可以防止SQL注入。",{"type":18,"tag":30,"props":3230,"children":3231},{},[3232],{"type":28,"value":3233},"运行上述代码，在DataGirp中查看books表数据，如图所示。",{"type":18,"tag":30,"props":3235,"children":3236},{},[3237],{"type":18,"tag":687,"props":3238,"children":3241},{"alt":3239,"src":3240},"Pasted image 20250721133636","\u002Fimages\u002Fblog\u002Fpython-basics\u002FPasted%20image%2020250721133636.png",[],{"type":18,"tag":3243,"props":3244,"children":3245},"style",{},[3246],{"type":28,"value":3247},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":7,"searchDepth":227,"depth":227,"links":3249},[3250,3254,3258],{"id":25,"depth":227,"text":25,"children":3251},[3252,3253],{"id":53,"depth":236,"text":53},{"id":436,"depth":236,"text":436},{"id":664,"depth":227,"text":667,"children":3255},[3256,3257],{"id":675,"depth":236,"text":675},{"id":887,"depth":236,"text":890},{"id":2121,"depth":227,"text":2124,"children":3259},[3260,3261,3262,3263],{"id":2127,"depth":236,"text":2130},{"id":2185,"depth":236,"text":2185},{"id":2436,"depth":236,"text":2436},{"id":2832,"depth":236,"text":2835},"markdown","content:blog:2025-07-12-python-Python操作数据库.md","content","blog\u002F2025-07-12-python-Python操作数据库.md","blog\u002F2025-07-12-python-Python操作数据库","md",1780969292504]