Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1076459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:25:45+00:00 2026-05-16T21:25:45+00:00

This post refers to this page for merging SQLite databases. The sequence is as

  • 0

This post refers to this page for merging SQLite databases.

The sequence is as follows. Let’s say I want to merge a.db and b.db. In command line I do the following.

  • sqlite3 a.db
  • attach ‘b.db’ as toM;
  • begin; <–
  • insert into benchmark select * from toM.benchmark;
  • commit; <–
  • detach database toM;

It works well, but in the referred site, the questioner asks about speeding up, and the answer is to use the ‘begin’ and ‘commit’ command.

Then, I came up with the following python code to do the exactly same thing. I abstract the SQLite function calls with SQLiteDB, and one of it’s method is runCommand(). I got the same error even though I delete the self.connector.commit().

# run command
def runCommand(self, command):
    self.cursor.execute(command)
    self.connector.commit() # same error even though I delete this line

db = SQLiteDB('a.db')
cmd = "attach \"%s\" as toMerge" % "b.db"
print cmd
db.runCommand(cmd)
cmd = "begin"
db.runCommand(cmd)
cmd = "insert into benchmark select * from toMerge.benchmark"
db.runCommand(cmd)
cmd = "commit"
db.runCommand(cmd)
cmd = "detach database toMerge"
db.runCommand(cmd)

But, I got the following error.

OperationalError: cannot commit - no transaction is active

Even though the error, the result db is well merged. And without the begin/commit, there’s no error at all.

  • Why can’t I run the begin/commit command?
  • Is it absolutely necessary to run begin/commit to safely merge the db files? The post says that the purpose of begin/commit is for speedup. Then, what’s the difference between using and not using the begin/commit command in terms of speedup?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:25:46+00:00Added an answer on May 16, 2026 at 9:25 pm

    Apparently, Cursor.execute doesn’t support the ‘commit’ command. It does support the ‘begin’ command but this is redundant because sqlite3 begins them for you anway:

    >>> import sqlite3
    >>> conn = sqlite3.connect(':memory:')
    >>> cur = conn.cursor()
    >>> cur.execute('begin')
    <sqlite3.Cursor object at 0x0104B020>
    >>> cur.execute('CREATE TABLE test (id INTEGER)')
    <sqlite3.Cursor object at 0x0104B020>
    >>> cur.execute('INSERT INTO test VALUES (1)')
    <sqlite3.Cursor object at 0x0104B020>
    >>> cur.execute('commit')
    
    Traceback (most recent call last):
      File "<pyshell#10>", line 1, in <module>
        cur.execute('commit')
    OperationalError: cannot commit - no transaction is active
    >>> 
    

    just use the commit method on your Connection object.

    As for your second question, it is not absolutely necessary to call begin/commit when merging the files: just be sure that there is absolutely no disk error, modifications to the db’s or people looking at the computer the wrong way while it is happening. So begin/commit is probably a good idea. Of course, if the original db’s aren’t being modified (I honestly haven’t looked) then there is no need for that even. If there is an error, you can just scrap the partial output and start over.

    It also provides a speedup because every change doesn’t have to be written to disk as it occurs. They can be stored in memory and written in bulk. But as mentioned sqlite3 handles this for you.

    Also, it’s worth mentioning that

    cmd = "attach \"%s\" as toMerge" % "b.db"
    

    is wrong in the sense that it’s depracated. If you want to do the wrong thing correctly, it’s

    cmd = 'attach "{0}" as toMerge'.format("b.db") #why not just one string though?
    

    This is forward compatible with newer versions of python which will make porting code easier.

    if you want to do the right thing, it’s

    cmd = "attach ? as toMerge"
    cursor.execute(cmd, ('b.db', ))
    

    This avoids sql injection and is, apparently, slightly faster so it’s win-win.

    You could modify your runCommand method as follows:

    def runCommand(self, sql, params=(), commit=True):
        self.cursor.execute(sql, params)
        if commit:
            self.connector.commit()
    

    now you can not commit after every single command by passing commit=False when you don’t need a commit. This preserves the notion of transaction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 542k
  • Answers 542k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use: SELECT p.id as a, p.url as b, t.id as… May 17, 2026 at 3:22 am
  • Editorial Team
    Editorial Team added an answer There are two parts to the problem First Issue You… May 17, 2026 at 3:19 am
  • Editorial Team
    Editorial Team added an answer I thought I'd show the regex approach, too. It doesn't… May 17, 2026 at 3:18 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

This is my first post, so first of all I want to say a
I just read this post about why new-line warnings exist, but to be honest
I refer to this page: Post array of multiple checkbox values <input type=checkbox class=box
This post reference to the One Definition Rule. Wikipedia is pretty bad on explaining
So this post talked about how to actually implement url rewriting in an ASP.NET
From this post . One obvious problem is scalability/performance. What are the other problems
I this post , I've seen this: class MonitorObjectString: public MonitorObject { // some
Background this post explains how one can consume extension methods in Powershell http://community.bartdesmet.net/blogs/bart/archive/2007/09/06/extension-methods-in-windows-powershell.aspx Compare
Reading this post has left me wondering; are nightly builds ever better for a
I have read this post about how to test private methods. I usually do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.