简体   繁体   English

使用sqlite数据库中的值在python中创建文本文件

[英]Create a text file in python using values from a sqlite database

I am trying to build a text file which is a combination of predefined strings and variable values which I would like to take from a pre-existing sqlite database. 我正在尝试构建一个文本文件,该文件是预定义的字符串和变量值的组合,我想从一个已有的sqlite数据库中获取该文件。 The general format of each line of the text file is as such: 文本文件每一行的一般格式如下:

constraint n: value i < value j 约束n:值i <值j

Where n is an integer which will increase by one every line. 其中n是一个整数,每行将增加一个。 Value i is the value found at row x, column y of table i and value j is that found at row x, column y of table j. 值i是在表i的第x行,第y列中找到的值,值j是在表j的第x行,第y列中找到的值。 I will need to iterate over each value of these tables presently in my database. 我将需要遍历数据库中当前这些表的每个值。 My goal is to use nested while loops in order to iterate through each value in the tables, along the lines of: 我的目标是使用嵌套的while循环来遍历表中的每个值,如下所示:

>>> while x < 30:
         y = 1
         while y < 30:
              #code to populate text file goes here
              n = n + 1
              y = y + 1
         x = x + 1

I have some limited experience in python but am wide open to any suggestions. 我在python方面经验有限,但是对任何建议都持开放态度。 Any input that anyone has would be very much appreciated. 任何人的任何投入将不胜感激。

Thank you very much and Happy New Year! 非常感谢您,新年快乐!

Paul 保罗

The simplest, unoptimized approach would be something like: 最简单,未优化的方法如下:

import sqlite3
conn = sqlite3.connect('whatever.file')
c = conn.cursor

out = open('results.txt', 'w')
n = 1

for x in range(1, 30):
  for y in range(1, 30):
    c.execute('select value from i where row=? and column=?', (x, y))
    i = c.fetchone()[0]
    c.execute('select value from j where row=? and column=?', (x, y))
    j = c.fetchone()[0]
    out.write('constraint %d: %s < %s\n' % (n, i, j))
    n += 1

out.close()
conn.close()

The main issue with this approach is that it uses 1682 separate queries to sqlite and thereby might be a bit slow. 这种方法的主要问题是,它使用1682个单独的查询来访问sqlite,因此可能会有点慢。 The main optimization would be to reduce the number of queries by fetching (eg) all i and j values for each given x with just two queries (by having no where condition on y and select value, column as the select subclause) -- whether you need it or not depends on whether the performance of this dirt-simple approach is already satisfactory for your purposes. 主要优化将是通过仅用两个查询(通过在y上没有where条件和select value, column作为select子句)获取(例如)每个给定x所有ij值来减少查询数量。您是否需要取决于这种简单方法的性能是否已经满足您的目的。

I believe you will be much happier if you use an ORM to access the data, rather than direct SQL as shown in Alex Martelli's answer. 我相信,如果您使用ORM来访问数据,而不是像Alex Martelli的答案中所示的那样直接使用SQL,那么您会更加高兴。 It seems like your database is very simple and an ORM will work well. 看来您的数据库非常简单,并且ORM可以正常工作。

I suggest you try SQLAlchemy or Autumn. 我建议您尝试使用SQLAlchemy或Autumn。 SQLAlchemy is powerful and flexible, and has been around for a long time. SQLAlchemy功能强大且灵活,已经存在了很长时间。 Autumn is very simple. 秋天很简单。 Either one should help you. 任一种都可以帮助您。

http://www.sqlalchemy.org/ http://www.sqlalchemy.org/

http://autumn-orm.org/ http://autumn-orm.org/

On the other hand, if Alex Martelli's example code has already solved your problem and this is a simple one-off problem, then hey, grab the solution and go. 另一方面,如果Alex Martelli的示例代码已经解决了您的问题,并且这是一个简单的一次性问题,那么,请抓住解决方案,然后继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM