将sqlite查询结果导入到文本文件
将sqlite查询结果导入到文本文件 下班前接到一个需求,要将sqlite数据库中的某个字段导出保存到一个文本中,数据大概有1000w左右,于是我就写了一个python脚本,来做这个事情。 www.2cto.com 01 #!/usr/xxx/bin/python 02 03 import os,sys 04 import sqlite
将sqlite查询结果导入到文本文件
下班前接到一个需求,要将sqlite数据库中的某个字段导出保存到一个文本中,数据大概有1000w左右,于是我就写了一个python脚本,来做这个事情。
www.2cto.com
01
#!/usr/xxx/bin/python
02
03
import os,sys
04
import sqlite3
05
import logging
06
07
sys.path.append('.')
08
# logger configure
09
logger = logging.getLogger()
10
handler = logging.FileHandler('/home/admin/tmp/xxx.txt')
11
logger.addHandler(handler)
12
logger.setLevel(logging.NOTSET)
13
14
sqlfile_path = '/home/admin/tmp/user.sql'
15
16
def write_to_file(guid):
17
total_count = get_total_count(guid)
18
page_size = 10000
19
offset_num = 0
20
op_num = 0
21
while op_num
22
array_list = get_data([guid,page_size,op_num])
23
offset_num = offset_num + 1
24
op_num = offset_num * page_size
25
for n in array_list:
26
logger.info(n)
27
28
def get_total_count(guid):
29
conn = sqlite3.connect(sqlfile_path)
30
cur = conn.cursor()
31
cur.execute('select count(*) from table_name where id = \'' + guid + '\'')
32
try:
33
count = cur.fetchone()[0]
34
except:
35
count = 0
36
cur.close()
37
conn.close()
38
return count
39
40
def get_data(item):
41
conn = sqlite3.connect(sqlfile_path)
42
cur = conn.cursor()
43
cur.execute('select name from table_name where id = \'' + item[0] + '\' limit ' + item[1] + ' offset ' + item[2])
44
array_list = []
45
for r in cur.fetchall():
46
array_list.append(r[0])
47
cur.close()
48
conn.close()
49
return array_list
50
51
if '__maiin__' == __name__:
52
for guid in ['123456789','987654321']:
53
write_to_file(guid)
考虑到数据还是有一点大的,所以就每次查询10000条操作,写完后运行起来,然后我就下班走人了,第二天上班发现这个脚本居然还在跑着,一晚上还没有结束。那叫一个头疼啊。。。 这不是 GC 。
既然这么慢,那我就慢慢等吧,然后开始做其他事情去了,GC来了,兄弟我在测试其他程序的时候,一个不小心,在运行脚本的时候,把python脚本跑出来的文本给删了,连python脚本也一起全删了,当我意识过来的时候,泪流满面啊。。。
就在我沮丧的时候,旁边一同学问了我情况,然后默默的给了我一行代码:
1 sqlite3 user.sql "select name from table_name where id = '123456789'" >> xxx.txt
之后十分钟,数据全导出来了,效率真TM高。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

When deleting or decompressing a folder on your computer, sometimes a prompt dialog box "Error 0x80004005: Unspecified Error" will pop up. How should you solve this situation? There are actually many reasons why the error code 0x80004005 is prompted, but most of them are caused by viruses. We can re-register the dll to solve the problem. Below, the editor will explain to you the experience of handling the 0x80004005 error code. Some users are prompted with error code 0X80004005 when using their computers. The 0x80004005 error is mainly caused by the computer not correctly registering certain dynamic link library files, or by a firewall that does not allow HTTPS connections between the computer and the Internet. So how about

When we use this platform to listen to songs, most of them should have some songs that you want to listen to. Of course, some things may not be listened to because there is no copyright. Of course, we can also directly use some songs imported locally. Go up there so you can listen. We can download some songs and directly convert them into mp3 formats, so that they can be scanned on the mobile phone for import and other situations. However, for most users, they don’t know much about importing local song content, so in order to solve these problems well, today the editor will also explain it to you. The content method allows you to make better choices without asking. If you are interested,

Quark Netdisk and Baidu Netdisk are currently the most commonly used Netdisk software for storing files. If you want to save the files in Quark Netdisk to Baidu Netdisk, how do you do it? In this issue, the editor has compiled the tutorial steps for transferring files from Quark Network Disk computer to Baidu Network Disk. Let’s take a look at how to operate it. How to save Quark network disk files to Baidu network disk? To transfer files from Quark Network Disk to Baidu Network Disk, you first need to download the required files from Quark Network Disk, then select the target folder in the Baidu Network Disk client and open it. Then, drag and drop the files downloaded from Quark Cloud Disk into the folder opened by the Baidu Cloud Disk client, or use the upload function to add the files to Baidu Cloud Disk. Make sure to check whether the file was successfully transferred in Baidu Cloud Disk after the upload is completed. That's it

Download the latest version of 12306 ticket booking app. It is a travel ticket purchasing software that everyone is very satisfied with. It is very convenient to go wherever you want. There are many ticket sources provided in the software. You only need to pass real-name authentication to purchase tickets online. All users You can easily buy travel tickets and air tickets and enjoy different discounts. You can also start booking reservations in advance to grab tickets. You can book hotels or special car transfers. With it, you can go where you want to go and buy tickets with one click. Traveling is simpler and more convenient, making everyone's travel experience more comfortable. Now the editor details it online Provides 12306 users with a way to view historical ticket purchase records. 1. Open Railway 12306, click My in the lower right corner, and click My Order 2. Click Paid on the order page. 3. On the paid page

How to check my academic qualifications on Xuexin.com? You can check your academic qualifications on Xuexin.com, but many users don’t know how to check their academic qualifications on Xuexin.com. Next, the editor brings you a graphic tutorial on how to check your academic qualifications on Xuexin.com. Interested users come and take a look! Xuexin.com usage tutorial: How to check your academic qualifications on Xuexin.com 1. Xuexin.com entrance: https://www.chsi.com.cn/ 2. Website query: Step 1: Click on the Xuexin.com address above to enter the homepage Click [Education Query]; Step 2: On the latest webpage, click [Query] as shown by the arrow in the figure below; Step 3: Then click [Login Academic Credit File] on the new page; Step 4: On the login page Enter the information and click [Login];

Recently, many netizens have asked the editor, what is the file hiberfil.sys? Can hiberfil.sys take up a lot of C drive space and be deleted? The editor can tell you that the hiberfil.sys file can be deleted. Let’s take a look at the details below. hiberfil.sys is a hidden file in the Windows system and also a system hibernation file. It is usually stored in the root directory of the C drive, and its size is equivalent to the size of the system's installed memory. This file is used when the computer is hibernated and contains the memory data of the current system so that it can be quickly restored to the previous state during recovery. Since its size is equal to the memory capacity, it may take up a larger amount of hard drive space. hiber

Detailed explanation of the role of .ibd files in MySQL and related precautions MySQL is a popular relational database management system, and the data in the database is stored in different files. Among them, the .ibd file is a data file in the InnoDB storage engine, used to store data and indexes in tables. This article will provide a detailed analysis of the role of the .ibd file in MySQL and provide relevant code examples to help readers better understand. 1. The role of .ibd files: storing data: .ibd files are InnoDB storage

MySQL and PL/SQL are two different database management systems, representing the characteristics of relational databases and procedural languages respectively. This article will compare the similarities and differences between MySQL and PL/SQL, with specific code examples to illustrate. MySQL is a popular relational database management system that uses Structured Query Language (SQL) to manage and operate databases. PL/SQL is a procedural language unique to Oracle database and is used to write database objects such as stored procedures, triggers and functions. same
