码迷,mamicode.com
首页 > 数据库 > 详细

SQLite

时间:2019-05-19 11:51:22      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:failed   import   就是   数据库文件   sel   bsp   blank   error   cal   

python接口开发:

import sqlite3

def createTable(conn):
    sql_create = ‘‘‘
    CREATE TABLE IF NOT EXISTS employees (
    id INT PRIMARY KEY NOT NULL,
    name TEXT NOT NULL,
    salary REAL
    )‘‘‘

    try:
        conn.execute(sql_create)
        return 0
    except:
        return 1

def insert(conn, id, name, salary):
    sql_insert = ‘‘‘
    INSERT INTO 
        employees(id, name, salary)
    SELECT
        ?, ?, ?
    WHERE NOT EXISTS(SELECT * FROM employees WHERE id=?)
    ‘‘‘

    try:
        conn.execute(sql_insert, (id, name, salary, id))
        print "insert a line of data successfully"
        return 0
    except:
        print "insert a line of data failed ! id = ", id
        return 1

def select(conn, id):
    sql_select = ‘‘‘
    SELECT 
    id, name, salary
    FROM
    employees
    WHERE
    id = ?
    ‘‘‘

    cursor = conn.execute(sql_select, (id,))
    print "select a line of data successfully"
    return cursor

def delete(conn, id, name, salary):
    sql_insert = ‘‘‘
    DELETE FROM 
    employees
    ‘‘‘

    try:
        conn.execute(sql_insert)
        print "delete data successfully"
    except:
        print "delete data failed ! "

# open or create database
conn = sqlite3.connect("employee.db")
print "Open database successfully"

if(0==createTable(conn)):
    print "createTabe successfully"
else:
    print "createTabe failed"
    conn.close()
    exit(-1)

delete(conn, 1, "rc", 4.0)
cursor = select(conn, 1)
print list(cursor)
cursor = select(conn, 2)
print list(cursor)

insert(conn, 1, "rc", 4.0)
insert(conn, 2, "mario", 0.0)
insert(conn, 2, "mario-mario", 0.0)

cursor = select(conn, 2)
print list(cursor)

print list(conn.execute("SELECT * FROM employees"))

# must commit, then the data will be written to database
conn.commit()
print "commit successfully"
conn.close()
print "close database successfully"

 

C接口开发:

官方sqlite3库下载地址:https://www.sqlite.org/download.html

四个基础API:

int sqlite3_open(
  const char *filename,   /* Database filename */
  sqlite3 **ppDb          /* OUT: SQLite db handle */
);
/*
打开/创建数据库文件的API,第一个参数是文件路径及名字,第二个参数是sqlite3操作句柄。如果数据库文件不存在就创建一个,并且会返回一个数据库操作句柄,保存到第二个参数中。操作成功,返回值为SQLITE_OK.
*/

int sqlite3_exec(
  sqlite3*,                                  /* An open database */
  const char *sql,                           /* SQL to be evaluated */
  int (*callback)(void*,int,char**,char**),  /* Callback function */
  void *,                                    /* 1st argument to callback */
  char **errmsg                              /* Error msg written here */
);
/*
执行sql语句。第一个参数,打开的数据库操作句柄。第二个参数,sql语句,就是我们要执行的命令。第三个参数,回调函数。第四个参数,传入回调函数的参数。第五个参数,保存操作失败时的错误信息。操作成功,返回值为SQLITE_OK.
*/

void sqlite3_free(void *);
/*
释放申请的动态内存了,在上一个API操作中,假设出现操作失败,错误信息保存在最后一个参数中,它的内存是动态申请的,这时候就要通过这个函数释放内存了。
*/

int sqlite3_close(sqlite3 *);
/*
关闭数据库。第一个参数就是sqlite3操作句柄。一般在结束或出错时调用,用来关闭数据库。
*/

 

SQLite

标签:failed   import   就是   数据库文件   sel   bsp   blank   error   cal   

原文地址:https://www.cnblogs.com/ruanchao/p/10888467.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!