Set PRAGMA encoding ="UTF-16" for main database in sqlite - database

I read that it is not possible to change the text encoding of a database after it has been created and any attempt to do so will be silently ignored.(from here)
Frome this post, to create main database,say 'databasename.db' in SQLite, we have to type following in command prompt :
sqlite3 databasename.db
which creates the database and displays sqlite shell as :
C:\Documents and Settings\Administrator>sqlite3 AudioData.db
SQLite version 3.7.11 2012-03-20 11:35:50
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>
In this case, how to set PRAGMA encoding ? How can I give PRAGMA command before create database command since the sqlite shell starts after create database command??
Thanks in advance!

You just call sqlite3 as you wrote. At this time there is just an empty file. Then you set your pragmas. The database with all its settings will be written after creating the first table. And the time of writing with all the environment variables is important. Just then the database is really created. So just set your pragmas before creating the first table.
e.g.
sqlite3 test.db
opens sqlite, nothing is written at that time
pragma encoding=utf16;
there is still nothing written
.quit
closes sqlite, an empty file test.db gets created
sqlite3 test.db
opens sqlite
pragma encoding = utf16;
pragma nnnn=xxx;
sets the pragma(s), the file test.db is still empty
create table test (testid integer);
now the database gets really created and the database file test.db gets written with content

Use SQLlite Browser for such advanced functionalities. It is very straight forward to use.

Related

Backup a sqlite3 database

I have a Python project where I use sqlite3 to save data.
I want to do a backup of the database ( I am really worried about datalock, cause my software will be used by like 10 peoples which will have to access to my database to write or read, and without luck, someone will try to access to the database at the bad moment, even if I will increase timeout)
If I do it manually with the Windows cmd, there is no problem.
v:
cd V:\directory\
sqlite3 mydatabase.db"
.backup backup_db.db
I try to do a batch file which will be call every hours by my python software to do it automatically.
I applied the same commands in it.
The batch file is launched, but the process stop after the opening of the database.
The dot command is not executed.
Where is my mistake?
Batch files do not work the way you think they do.
You currently think that after the line sqlite3 "mydatabase.db" you are in some kind of "database mode" and all the following lines in the batch file are passed to the sqlite3 process.
That's not the case.
Every line in a batch file is executed after the previous like has finished running.
The line sqlite3 "mydatabase.db" starts the sqlite3 process... and then waits until this process exits. Which never happens unless you do it manually by pressing a key.
And after that cmd.exe will try to execute the command .backup backup_db.db, but since that's not a command cmd.exe understands, it will fail with an error ("'.backup' is not recognized as an internal or external command, operable program or batch file.").
What you really want to do is create a script file and pass it to for SQLite for processing. This can be done by
redirecting a file into the sqlite3 process. Assume that create_backup.txt contains the commands to create a backup:
sqlite3 "mydatabase.db" < create_backup.txt
piping the file into the sqlite3 process, e.g. using type:
type create_backup.txt | sqlite3 "mydatabase.db"
alternatively you can use echo to output a command:
echo .backup backup_db.db | sqlite3 "mydatabase.db"
Obviously the echo method is easier for one-liners whereas the input redirect with < or writing out a file with type are easier to for more complex, multi-line operations.
Using the -init parameter of sqlite3.exe is another option you can try. Check the SQLite documentation.
The syntax for the sqlite3 executable is sqlite3 [db_file] [command]. So, in your batch file, you should include .backup on the same line.
v:
cd V:\directory\
sqlite3 mydatabase.db ".backup backup_db.db"

Check if a file exists in UNIX from SQLplus without UTL_FILE

My current scenario is like this:
I need to login to sqlplus from a shell script to call a stored procedure.
After that I need to create a CSV file by SPOOLING data from a table.
Then I need to check whether the CSV file has been created in a particular directory and depending on the result an update query needs to be run.
I know that this can be checked within sqlplus with the help of UTL_FILE package but unfortunately due to Client policies,the access of this package is restricted in the current system.
Another way is to exit from sqlplus and perform the file check in UNIX and then again log in to sqlplus to perform the rest actions. But this I believe would result in slower execution time and performance is an important factor in this implementation as the tables contain huge volumes of data(in millions).
So is there any other way to check this from sqlplus without exiting from the current session?
System Info:
OS - Red Hat Enterprise Linux
Database - Oracle 11g
If the file is on the same machine that you're running SQL*Plus on, you could potentially use the host command.
If the file you're checking is the same one you're spooling to, it must exist anyway, or you would have got an SP error of some kind; but if you do want to check the same file for some reason, and assuming you have a substitution variable with the file name:
define csv_file=/path/to/spool.csv
-- call procedure
spool &csv_file
-- do query
spool off
host ls &csv_file
update your_table
set foo=bar
where &_rc = 0;
If the file exists when the host command is run, the _rc substitution variable will be set to zero. If the file doesn't exist or isn't readable for any reason it will be something else - e.g. 2 if the file just doesn't exist. Adding the check &_rc = 0 to your update will mean no rows are updated if there was an error. (You can of course still have whatever other conditions you need for the update).
You could suppress the display of the file name by adding 1>/dev/null to the host command string; and could also suppress any error messages by also adding 2>/dev/null, though you might want to see those.
The documentation warns against using &_rc as it isn't portable; but it works on RHEL so as long as you don't need your script to be portable to other operating systems this may be good enough for you. What you can't do, though, is do anything with the contents of the file, or interpret anything about it. All you have available is the return code from the command you run. If you need anything more sophisticated you could call a script that generates specific return codes, but that's getting a bit messy.

create flat file in ssis package

I'm working on creating a csv export from a SQL Server database and I've been familiar with a process for doing so that admittedly, I've never completely understood. The process involves creating a "template" file, which defines the columns and structure for the file export. Once the "template" file exists, you can use a Data Flow task to fill it and a File System Task to copy it to the final storage destination with whatever file name you'd like (frequently a date/time stamp).
Is there a reason that you can't simply create a file directly, without the intermediate "template" file? I've looked around for a bit and it seems like all the proposed solutions involve connecting to an existing file. I see that there is a "Create File" Usage type for a "File" connection manager, but you can't use it in any File System Task. The only File System Type connection managers you can use relative to a file are "Copy", "Delete", "Move", "Rename", and "Set Attributes".
Is there a way to create a file at package run time and fill it?
The whole point of SSIS is to create a data flow with metadata so that the data can be manipulated - if you just want to go database direct to CSV you are probably better off using bcp (bulk copy program) from the command line. If you want to include it as part of a SSIS package just add an Execute Process Task and add the command line to that. You can dynamically change the included columns or the output file by adding an expression to the task. You could also call bcp though TSQL using an Excute SQL Task.
One other option is to concatenate all your columns in your query inter-spaced with a comma literal and output to a text file with just one very wide column.
For documentation on bcp look here

Mysql import error from bash script

i have a big database dump (290mb). When i use import from cli everything is ok!
mysql -u root -piddqd whitestore_com < whitestore_com.sql
When i use the same command from bash script like:
./build.sh
Here is what i get:
? (\?) Synonym for `help'.
clear (\c) Clear the current input statement.
connect (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit (\e) Edit command with $EDITOR.
ego (\G) Send command to mysql server, display result vertically.
exit (\q) Exit mysql. Same as quit.
go (\g) Send command to mysql server.
help (\h) Display this help.
nopager (\n) Disable pager, print to stdout.
notee (\t) Don't write into outfile.
pager (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print (\p) Print current command.
prompt (\R) Change your mysql prompt.
quit (\q) Quit mysql.
rehash (\#) Rebuild completion hash.
source (\.) Execute an SQL script file. Takes a file name as an argument.
status (\s) Get status information from the server.
system (\!) Execute a system shell command.
tee (\T) Set outfile [to_outfile]. Append everything into given outfile.
use (\u) Use another database. Takes database name as argument.
charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax
to use near 'mysql Ver 14.14 Distrib 5.1.66, for debian-linux-gnu (x86_64)
using readline 6.' at line 1
I dont know why I get this error, there is no such string as "mysql Ver 14.14 Distrib 5.1.66, for debian-linux-gnu (x86_64) in dump file
using readline 6."
Try opening up the whitestore_com.sql in a text editor and check the first couple of lines.
It looks like there's some text at the start of the .sql file that should be commented out.
It's opening up the mysql connection ok, but then the commands it's getting from whitestore_com.sql aren't valid SQL statements.

Suppress reorg rebuild sysmessages in sybase stored proc

I have a stored procedure in Sybase that uses reorg rebuild statement in a loop for all the tables in my database. What I want to do is to suppress the reorg rebuild sysmessages for tables that succedeed the procedure and only to print the tables that were locked etc...thus the problematic ones....The thing is that I did not succeed to find out anything to use in manual or in any workshops...dow you have any idea?
Thanks in advance !!!!!
If you are running the SQL with isql at a command prompt, you can always capture the output in a text file and filter it out with other tools.
Create a script to run the SQL in isql and then use a script that calls a text processing tool (awk,sed,...) to only find the lines of interest.
Here is an example from a windows batch file with a regex that removes lines that start with a space (i.e. Rows Effected messages)
isql -SDBDEV1 -DMyDbName -U%DBLOG% -P%DBPWD% -iLoadBatchStats.sql -o%TEMP%\LoadBatchStats.log
type %TEMP%\LoadBatchStats.log | gawk "/^[ ]/{print $0}" >>%TEMP%\LoadBatchSummary.log

Resources