Python SQLite (1)

Source: Internet
Author: User
Tags python sqlite

#!/usr/bin/env python# Coding=utf-8import sqlite3conn = Sqlite3.connect (": Memory:") C = conn.cursor () c.execute (    ' ' ' CREATE TABLE Stocks (data Text,trans text,symbol test,qty real,price Real) ' C.execute ("INSERT into Stocks VALUES (' 2006 -01-05 ', ' BUY ', ' rhat ', 100,35.14) ') conn.commit () symbol = ' rhat ' C.execute ("SELECT * from Stocks WHERE symbol= '%s '"% Symbol) print c.fetchone () parament = [    (' 2014-11-11 ', ' wcs ', ' Rhat ', $, 20.5),    (' 2014-11-15 ', ' wcs ', ' rhat ', 30.0)]# for T in parament:#    c.execute (' INSERT INTO stocks VALUES (?,?,?,?,?) ', T) c.executemany ("INSERT INTO stock S VALUES (?,?,?,?,?) ", parament) C.execute (" SELECT * from Stocks WHERE symbol= '%s ' "% symbol) print C.fetchall (

  

Python is a lightweight development database that is simpler and more intuitive compared to SQL Server and roacle, such as Oracle's huge installation file SQLite is a lot lighter, Take Python. When you install Python, you automatically install SQLite, and users simply load it into the project via import and use it directly.

When you call SQLite, you need to load the Sqlite3 via import, and when you are finished loading it, you can directly call its methods to perform the corresponding operations on the database.

SQLite has two modes of loading:

1 Memory Form

All of the data under this type is stored in memory, which is equivalent to creating a dedicated area in memory when the user runs the program, and automatically emptying all data in its area when the project is finished, and faster access relative to the file.

When you call the. Connect () method, you can create a memory database simply by calling ": Memory:" As a parameter call

2. File Form

. Connect ("filename") is automatically loaded when the system already has a database of the corresponding name, and is created automatically when there is no database.

If you need to perform the appropriate data operation, you need to call its. Execute () method

1. Build table operation

    1. c.execute(

    2.     ‘‘‘CREATE TABLE stocks(data text,trans text,symbol test,qty real,price real)‘‘‘)

The above statement is a SQL statement that creates a table with the table named stocks

2. Insert operation

2.1: Using tuples to implement multiple data insertions at the same time

Parament = [(' 2014-11-11 ', ' wcs ', ' Rhat ', Max, 20.5), (' 2014-11-15 ', ' wcs ', ' rhat ', ' 30.0 ')]for T in Parament: C.execute ("INSERT into stocks VALUES (?,?,?,?,?)", T) c.execute ("SELECT * from Stocks WHERE symbol= '%s '"% symbol) Print C. Fetchall ()

(U ' 2006-01-05 ', U ' BUY ', U ' rhat ', 100.0, 35.14)

[(U ' 2006-01-05 ', U ' BUY ', U ' rhat ', 100.0, 35.14), (U ' 2014-11-11 ', U ' wcs ', U ' rhat ', 200.0, 20.5), (U ' 2014-11-15 ', U ' wcs ', U ' Rhat ', 300.0, 30.0)]

The first one is the initial state, and the second one inserts the subsequent query

2.2 Inserting multiple statements with Executemany

  1. Parament = [(' 2014-11-11 ', ' wcs ', ' Rhat ', $, 20.5), (' 2014-11-15 ', ' wcs ', ' rhat ', 300, 30.0)]

  2. c.executemany("INSERT INTO stocks VALUES(?,?,?,?,?)", parament)

  3. c.execute("SELECT * FROM stocks WHERE symbol=‘%s‘" % symbol)

  4. print c.fetchall(

The result is the same as 2.1

Python SQLite (1)

Related Article

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.