Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 仅从数据库获取更新的数据_Python_Sqlite_Python 2.7_Pyodbc - Fatal编程技术网

Python 仅从数据库获取更新的数据

Python 仅从数据库获取更新的数据,python,sqlite,python-2.7,pyodbc,Python,Sqlite,Python 2.7,Pyodbc,我必须从数据库中获取最近更新的数据。为了解决这个问题,我将上次读取的行号保存到python的shelve中。下面的代码适用于一个简单的查询,如select*from rows。我的代码是: from pyodbc import connect from peewee import * import random import shelve import connection d = shelve.open("data.shelve") db = SqliteDatabase("data.db

我必须从数据库中获取最近更新的数据。为了解决这个问题,我将上次读取的行号保存到python的
shelve
中。下面的代码适用于一个简单的查询,如
select*from rows
。我的代码是:

from pyodbc import connect
from peewee import *
import random
import shelve
import connection


d = shelve.open("data.shelve")
db = SqliteDatabase("data.db")
class Rows(Model):
    valueone = IntegerField()
    valuetwo = IntegerField()

    class Meta:
        database = db

def CreateAndPopulate():
    db.connect()
    db.create_tables([Rows],safe=True)
    with db.atomic():
        for i in range(100):
            row = Rows(valueone=random.randrange(0,100),valuetwo=random.randrange(0,100))
            row.save()
    db.close()

def get_last_primay_key():
    return d.get('max_row',0)

def doWork():
    query = "select * from rows" #could be anything
    conn = connection.Connection("localhost","","SQLite3 ODBC Driver","data.db","","")
    max_key_query = "SELECT MAX(%s) from %s" % ("id", "rows")
    max_primary_key = conn.fetch_one(max_key_query)[0]
    print "max_primary_key " + str(max_primary_key)
    last_primary_key = get_last_primay_key()
    print "last_primary_key " + str(last_primary_key)
    if max_primary_key == last_primary_key:
        print "no new records"
    elif max_primary_key > last_primary_key:
        print "There are some datas"
        optimizedQuery = query + " where id>" + str(last_primary_key) 
        print query
        for data in conn.fetch_all(optimizedQuery):
            print data
        d['max_row'] = max_primary_key
    # print d['max_row']

# CreateAndPopulate() # to populate data
doWork()
虽然代码可以用于没有
where
子句的简单查询,但是查询可以是从简单到复杂的任何查询,包括
连接
和多个
where
子句。如果是这样,那么我正在添加
where
的部分将失败。无论查询是什么,我如何从数据库中只获取最后更新的数据


PS:我不能修改数据库。我只需要从中提取。

使用偏移子句。例如:

SELECT * FROM [....] WHERE [....] LIMIT -1 OFFSET 1000

在查询中,将
1000
替换为绑定到
shelve
变量的参数。这将跳过顶部的“搁置”行数,只抓取较新的行。您可能希望最终考虑更健壮的重构,但祝您好运。

使用偏移子句。例如:

SELECT * FROM [....] WHERE [....] LIMIT -1 OFFSET 1000

在查询中,将
1000
替换为绑定到
shelve
变量的参数。这将跳过顶部的“搁置”行数,只抓取较新的行。你可能想最终考虑一个更健壮的重构,但运气好。

你说的“失败”是什么意思?你得到了什么错误的结果?一个例子是有用的……考虑一个类似于SQL的代码>代码>从…代码>,现在添加
其中id>。
不会给我想要的结果你说的“失败”是什么意思?你得到了什么错误的结果?一个例子是有用的……考虑一个类似于SQL的代码>代码>从…代码>,现在添加
where id>。
不会根据我使用的链接
limit-1 offset 1000
给出我想要的结果。我将尝试一下这段代码,让您知道我已经修改了我的答案-我已经有一段时间没有直接使用SQLite了,并且忘记了限制是必需的。基于我使用的链接
LIMIT-1 offset 1000
。我将尝试一下这段代码,让您知道我已经修改了我的答案——我已经有一段时间没有直接使用SQLite了,并且忘记了需要限制。