Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Development

Notices

Reply
 
Thread Tools Search this Thread
Old 09-14-2014, 07:36 PM   #1
Pinguin44
Junior Member
Pinguin44 began at the beginning.
 
Posts: 2
Karma: 10
Join Date: Sep 2014
Device: Kobo Aura
Sql for making a lot of change in Calibre database

Hello,

I know that we can made some change on selected items with a regex but that really take too much time for multiple update. The way for me it's to make multiple change on author, title, publisher, tags as a batch with Sql request.

So i search on this forum and other source on Google but i don't find so many solution. I found one good way here:
https://www.mobileread.com/forums/sho...&highlight=sql

I build a short py script for test:
Code:
from __future__ import (unicode_literals, division, absolute_import,
                        print_function)
from calibre.library.database2 import LibraryDatabase2
db = LibraryDatabase2('/Users/Bernard/Calibre Library Temp')
db.execute('UPDATE authors SET name="Karl Herbert Scheer" WHERE name="KH.SCHEER"')
db.execute('UPDATE authors SET name="Alfred Elton van Vogt" WHERE name="Alfred E. Van Vogt"')
db.execute('UPDATE authors SET name="Alfred Elton van Vogt" WHERE name="AE Van Vogt"')
When i call this script on terminal, i get this:
Code:
iMac-de-xxx:~ Bernard$ calibre-debug -e /Users/Bernard/Desktop/calibre-auto-correct.py 
Python function terminated unexpectedly: 'LibraryDatabase2' object has no attribute 'execute'
Traceback (most recent call last):
  File "/Applications/calibre.app/Contents/Resources/Python/lib/python2.7/site.py", line 208, in main
    return run_entry_point()
  File "/Applications/calibre.app/Contents/Resources/Python/lib/python2.7/site.py", line 114, in run_entry_point
    return getattr(pmod, func)()
  File "site-packages/calibre/debug.py", line 269, in main
  File "site-packages/calibre/debug.py", line 200, in run_script
  File "/Users/Pinguin/Desktop/calibre-auto-correct.py", line 6, in <module>
    db.execute('UPDATE authors SET name="Karl Herbert Scheer" WHERE name="KH.SCHEER"')
AttributeError: 'LibraryDatabase2' object has no attribute 'execute'
iMac-de-xxx:~ Bernard$
Perhaps i miss something somewhere !

Please help me to correct this little py script or give me another working solution to use Sql request with calibre.

Thank you.

Bernard
Pinguin44 is offline   Reply With Quote
Old 09-14-2014, 10:24 PM   #2
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,856
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
You absolutely should not use SQL to make changes to the calibre database.

Do this instead:

Code:
from calibre.library import db
db = db().new_api
books = db.search('authors:"=KH.SCHEER"')
db.set_field('authors', {book_id:'Karl Herbert Scheer' for book_id in books})
Using SQL to change authors will, for instance, mean that the filenames of the actual ebook files will not be changed to match the new author names. The values of author_sort in the authors table will not be changed and so on. Generally speaking, calibre's database is not a wrapper around sqlite. It has far more features than sqlite, so using SQL directly will cause all sorts of problems.

Last edited by kovidgoyal; 09-14-2014 at 11:11 PM.
kovidgoyal is offline   Reply With Quote
Advert
Old 09-14-2014, 11:17 PM   #3
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,856
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
And if you wish to directly rename an author:

Code:
author_id = db.get_item_id('authors', 'KH.SCHEER')
db.rename_items('authors', {author_id: 'Karl H. Scheer'})
which will automatically take care of renaming files and doing all other housekeeping for you.
kovidgoyal is offline   Reply With Quote
Old 09-15-2014, 12:27 PM   #4
Pinguin44
Junior Member
Pinguin44 began at the beginning.
 
Posts: 2
Karma: 10
Join Date: Sep 2014
Device: Kobo Aura
Wow ! Fast and great reply

Thank you very much. I made a test with the db.rename_items function. Everything work fine

For all users perhaps it should be great to have an 'auto-correct function' in a futur release of Calibre. Not for all field but just for author and editor. For sample in my library i have:

Alfred E. Van Vogt - Alfred E Van Vogt - AE Van Vogt - A.E. Van Vogt - A. E. van Vogt - Alfred Elton van Vogt for the same author !

J.-C. Lattés - JC Lattès - JCL - JCLattès - Jean-Claude Lattès - Editions Jcl for the same editor !

Think the best way is to have a grid with the destination field as 1st column, the strict string to find in column 2 (I don't think that RegEx is useful for that), and to finish the replacement string in column 3.

Ok easy to say but not to do... Perhaps someone can build that kind of extension.

Thank you again for the working script.

Bernard
Pinguin44 is offline   Reply With Quote
Old 09-15-2014, 12:47 PM   #5
BetterRed
null operator (he/him)
BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.
 
Posts: 20,568
Karma: 26954694
Join Date: Mar 2012
Location: Sydney Australia
Device: none
Quote:
Originally Posted by Pinguin44 View Post

Alfred E. Van Vogt - Alfred E Van Vogt - AE Van Vogt - A.E. Van Vogt - A. E. van Vogt - Alfred Elton van Vogt for the same author !

J.-C. Lattés - JC Lattès - JCL - JCLattès - Jean-Claude Lattès - Editions Jcl for the same editor !
The Manage Authors feature and Quality Check PI might be of assistance in straightening out disjoint author names after which it should be relatively easy to use the now 'established' name. There's a Tweak for how initials are to be dealt with.

Quote:
Originally Posted by Pinguin44 View Post
Ok easy to say but not to do... Perhaps someone can build that kind of extension.
Most extensions/plug-ins are built by the first person who needed it

BR
BetterRed is offline   Reply With Quote
Advert
Old 09-15-2014, 01:33 PM   #6
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 11,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by BetterRed View Post
The Manage Authors feature and Quality Check PI might be of assistance in straightening out disjoint author names after which it should be relatively easy to use the now 'established' name. There's a Tweak for how initials are to be dealt with.
Exactly what I was going to say. How is building a plugin or SQL scaffolding better than simply right-clicking on the authors in the tag browser and renaming them to what you want? Copy the "correct" name to the clipboard, use the tag browser search feature to find variants, and fix them.
chaley is offline   Reply With Quote
Old 09-15-2014, 01:59 PM   #7
BetterRed
null operator (he/him)
BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.
 
Posts: 20,568
Karma: 26954694
Join Date: Mar 2012
Location: Sydney Australia
Device: none
Quote:
Originally Posted by chaley View Post
Exactly what I was going to say. How is building a plugin or SQL scaffolding better than simply right-clicking on the authors in the tag browser and renaming them to what you want? Copy the "correct" name to the clipboard, use the tag browser search feature to find variants, and fix them.
But if you don't know about Manage Authors, QC plugins and Tweaking Initials then... It's not as if they're front and centre. But that's true of Photoshop, Word, Firefox... even my console emulator has more settings than at which you can poke a stick

Besides lashing some SQL and Python together is 'way cooler' than clicking a button or two

BR
BetterRed is offline   Reply With Quote
Old 12-26-2015, 08:40 AM   #8
tieum
Junior Member
tieum began at the beginning.
 
Posts: 1
Karma: 10
Join Date: Dec 2015
Device: Booken Muse
sql update

Quote:
Originally Posted by Pinguin44 View Post

Code:
AttributeError: 'LibraryDatabase2' object has no attribute 'execute'
Hi, I encountered the same issue for a sql update today: after a massive import on my new calibre-server, I wanted to set the timestamp to the published date

If anyone is stuck like I was: you can call execute() through db.conn

e.g.:
Code:
calibre-debug -c "from calibre.library.database2 import LibraryDatabase2;
db = LibraryDatabase2('/home/path/to/my/calibre-library/');
db.conn.execute('update books set timestamp=pubdate');
db.conn.commit();"
Happy holidays!
tieum is offline   Reply With Quote
Reply

Tags
calibre, database, request, script, sql


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Making P.Clark library database SBT Upload Help 6 04-04-2013 11:43 AM
What is the best way to defragment the SQL database? JSWolf Calibre 3 05-16-2012 08:49 AM
iPad BusinessInsider: Marvel iPad App Is Stunning And Making A Lot of Money Too kjk Apple Devices 0 06-18-2010 12:02 PM
Using SQL against Calibre database jjansen Calibre 3 03-29-2010 10:14 AM


All times are GMT -4. The time now is 08:47 PM.


MobileRead.com is a privately owned, operated and funded community.