Python Learning Seventh SQL optimization

Source: Internet
Author: User

Querying a single statement: Used to get a list of users and individual users

>>> fields=["id", "name", "NAME_CN", "email", "mobile"]                 >>> sql= "select %s from  users where name = ' admin '  %  ', '. Join (Fields) >>> sql "Select id, name,name_cn,email,mobile from users where name = ' admin ' >>>  Cur.execute (SQL) 1l>>> res=cur.fetchone () >>> res (16l, u ' admin ',  u ' admin ',  u ' 134556 ',  u ' 123455 ') >>> user={}>>> for i,k in enumerate (fields): ...     user[k]=res[i]... >>> user{' mobile ':  u ' 123455 ',   ' email ':  u ' 134556 ',  ' name_cn ':  u ' admin ',  ' id ': 16l,  ' name ':  u ' admin '} >>> for i,k in enumerate (Fields): ...      print i,k    i  is the index value, K is the FThe value inside the ields  ...  0 id1 name2 name_cn3 email4 mobile>>> res[1]u ' Admin

Insert statement: Add user

Version 1.0

>>> fields=["id", "name", "NAME_CN", "email", "mobile", "role" >>> fields[' id ', ' name ', ' name_cn ', ' Email ', ' mobile ', ' role ']>>> sql= ' insert into users (%s) values (' Fujinzhou ', ' Fujinzhou ', ' [email protected] ', ' 136** ', ' Ops ') "% ', '. Join (fields) >>> SQL" INSERT into users (id,name,name_cn,email,mobile,role) VALUES (' Fujinzhou ', ' Fujinzhou ', ' [email protected] ', ' 136** ', ' Ops ')

Version 2.0

>>> fields=["name", "NAME_CN", "email", "mobile", "role" >>> data={"name": "Fujinzhou"}>>> data{' name ': ' Fujinzhou '}>>> data={"name": "Fujinzhou", "NAME_CN": "Fujinzhou", "email": "[email protected]", "Mobile": "1122", "Role": "Ops"}>>> sql= "insert into users (%s) values%s"% (', '. Join (Fields), ', '. Join ([' "'%s '% DATA[K]) >>> SQL ' insert into users (name,name_cn,email,mobile,role) values "Fujinzhou", " Fujinzhou "," [email protected] "," 1122 "," Ops "


Version 3.0

>>> fields=[]>>> values=[]>>> data={"name": "Fujinzhou", "NAME_CN": "Fujinzhou", "email": "[ Email protected] "," mobile ":" 1122 "," Role ":" Ops "} >>> for K,v in Data.items (): ... fields.append (k) ... Valu Es.append ("'%s '"% v) ... >>> fields[' mobile ', ' role ', ' name_cn ', ' name ', ' email ']>>> values[' ' 1122 ', "' Ops '", "' Fujinzhou '", "' Fujinzhou '", "' [email protected] '"]>>> sql= "insert into users (%s) values (%s)"% (', '. Join (Fields), ', '. Join (values) >>> SQL "INSERT into users (Mobile,role,name_cn,name,email) VALUES (' 1122 ', ' Ops ', ' Fujinzhou ', ' Fujinzhou ', ' [email protected] '


This article from "Do not abandon!" Do not give up "blog, declined reprint!"

Python learning seventh SQL optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.