473,796 Members | 2,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strings with null bytes inside sqlite

I want to store strings in a sqlite database, but my strings may contain
null bytes and sqlite can't handle this. What is the best way to solve the
problem (the fastest in execution time and / or the one that produces the
least additional bytes) : base64, or is there a better solution ?

Thanks,
Pierre
Jul 18 '05 #1
2 2939
It depends on the structure of the string, but using
s.encode("strin g-escape") / e.decode("strin g-escape") may give better
performance and better storage characteristics (if your data is mostly
ASCII with a few NULs included). If chr(0) is the only problem value,
then creating your own encoder/decoder may be better, translating
'\0' -> '\\0' and '\\' -> '\\\\'.

Jeff

Jul 18 '05 #2
Jeff Epler wrote:
It depends on the structure of the string, but using
s.encode("strin g-escape") / e.decode("strin g-escape") may give better
performance and better storage characteristics (if your data is mostly
ASCII with a few NULs included). If chr(0) is the only problem value,
then creating your own encoder/decoder may be better, translating
'\0' -> '\\0' and '\\' -> '\\\\'.


No need to mess around with this yourself, as PySQLite provides native support for
binary data:
import sqlite
cx = sqlite.connect( ":memory:")
cu = cx.cursor()
cu.execute("cre ate table test(b binary)")
bindata = "".join([chr(x) for x in range(10)])
cu.execute("ins ert into test(b) values (%s)", (sqlite.Binary( bindata),))
cu.execute("sel ect b from test")
row = cu.fetchone()
row[0] == bindata

True

The PySQLite binary type uses a highly space efficient algorithm from the SQLite
author to encode chr(0) characters.

Yet another undocumented feature, I suppose. Unfortunately, I'm still offline at
home due to moving to a new appartment so the next release will have to wait even
longer.

-- Gerhard

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
5781
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT" >>>c = extract(a,b) >>>print c "abcdefghijklmnop"
16
2439
by: Paul Prescod | last post by:
I skimmed the tutorial and something alarmed me. "Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold any arbitrary data, even binary data such as photos and movies.They are of course also good for their traditional role of storing and manipulating text." This view of strings is about a decade out of date with modern programmimg practice. From...
5
1737
by: Maury Markowitz | last post by:
I have a byte returned from a DLL that contains n c-style strings inside it. Any suggestions on how to easily pull them out into a string? Encoding helps with a single byte array (although easily enough), but doesn't parse out runs of them. Looping over the bytes looking for x00's strikes me as too 1960s. Is there some helper that does this?
14
1775
by: Nick Z. | last post by:
I have a file that I want to read a UTF-16 unicode string from. What is the easiest way to accomplish that? Thanks in advance, Nick Z.
10
1850
by: Bonj | last post by:
Hello. I hope somebody can help me on this, because I'm running out of options to turn to. I have almost solved my regular expression function. Basically it works OK if unicode is defined. It doesn't work OK in ANSI mode however, as it has to use MultiByteToWideChar and WideCharToMultiByte. I've discovered that the regular expression part is working fine. As far as I can tell the regular expression code is correctly parsing what it...
89
5171
by: scroopy | last post by:
Hi, I've always used std::string but I'm having to use a 3rd party library that returns const char*s. Given: char* pString1 = "Blah "; const char* pString2 = "Blah Blah"; How do I append the contents of pString2 to pString? (giving "Blah Blah Blah")
11
6258
by: rossum | last post by:
I want to declare a const multi-line string inside a method, and I am having some problems using Environment.NewLine. I started out with: class foo { public void PrintStuff() { const string multiline = "The first line." + Environment.NewLine +
2
3819
by: Taras_96 | last post by:
Hi everyone, I've been told that PHP strings are null terminated. If PHP strings are in fact null terminated, I'm a bit puzzled how the following code can work: $filename = 'ucs2_file.txt'; $contents = file_get_contents($filename); echo "Echoing file contents\n"; echo $contents."\n";
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9533
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10461
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10239
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10190
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9057
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5447
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.