Results 1 to 6 of 6
  1. #1
    spider87 is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default SQLite and JAVA Charset settings

    What is the default charset in JAVA? (little-endian UTF-16 or big-endian UTF-16 or something else?)

    I want to work with latin characters in JAVA and SQLite (for example: ű etc...)
    I set UTF8 "pragma encoding" in sqlite (pragma encoding="UTF-8") but JAVA couldn't show latin characters.

    After that, I set the "JAVA_TOOL_OPTIONS" environment variable in Windows 7:
    Name:JAVA_TOOL_OPTIONS
    Value:-Dfile.encoding=UTF8


    when I tried to read lines from file, I saw the latin characters but it didn't work in sqlite

    I use this library: https://bitbucket.org/xerial/sqlite-jdbc

    here is my code:

    public void tesztconnect() throws SQLException
    {
    try {
    Connection connection = null;
    ResultSet resultSet = null;
    Statement statement = null;
    Class.forName("org.sqlite.JDBC");
    connection = DriverManager.getConnection("jdbc:sqlite:C:\\Sqlit e\\test.db");
    statement = connection.createStatement();
    resultSet = statement
    .executeQuery("SELECT * FROM words");
    while (resultSet.next())
    {
    System.out.println("WORD:"+ resultSet.getString("test2"));
    }
    } catch (ClassNotFoundException ex) {
    Logger.getLogger(DAL_sqlite.class.getName()).log(L evel.SEVERE, null, ex);
    }
    }


    What's the problem for this?
    this is the database schema: CREATE TABLE words (test1 string, test2 string);
    Select * from words: teststring|aeo
    when I run testconnect method I saw this: "WORD:a e o "
    Why couldn't I see "" characters?

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,352
    Rep Power
    6

    Default Re: SQLite and JAVA Charset settings

    Strings in Java are always UTF-16. Any other encoding is entirely context-sensitive, there are no defaults. The default encoding used for database interactions is defined by the driver.

    And regarding encoding problems: it may well be that the encoding of wherever you DISPLAY the data is incorrect, the encoding of the data itself may be just fine. Welcome to encoding-hell, stay a while and listen.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    spider87 is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Re: SQLite and JAVA Charset settings

    So... JAVA String always UTF-16... It's ok but...
    I created another database in SQLite
    before I created a new table I set UTF-16 "pragma encoding".
    after that I created a new table.
    when I use "select * from tablename" command,
    I saw wrong characters...
    Maybe UTF-16 couldn't show latin characters.

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,352
    Rep Power
    6

    Default Re: SQLite and JAVA Charset settings

    The internal encoding of a Java String says nothing about which encoding will be used by the database driver to store the data though, so I don't know why you are doing what you are doing. I would investigate encoding in the context of sqlite if I were you, to really learn what you should and shouldn't do in stead of just trying things.

    And again: be 100% sure the problem is not with whatever tool you are using to display the data. If you store stuff as UTF-16 and whatever you display the data on expects it to be another encoding entirely, things may go wrong.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,017
    Rep Power
    6

    Default Re: SQLite and JAVA Charset settings

    Quote Originally Posted by gimbal2 View Post
    And again: be 100% sure the problem is not with whatever tool you are using to display the data. If you store stuff as UTF-16 and whatever you display the data on expects it to be another encoding entirely, things may go wrong.
    Too much time working with ASCII. I tend to forget that Java encodes chars and Strings a UTF-16. Then I wonder why every other "character" in my output file is null (or the file is twice as big as I expected). :)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default Re: SQLite and JAVA Charset settings

    Quote Originally Posted by jim829 View Post
    Too much time working with ASCII. I tend to forget that Java encodes chars and Strings a UTF-16. Then I wonder why every other "character" in my output file is null (or the file is twice as big as I expected). :)
    You should write your files encoded as as UTF-8; that's a real superset encoding of ASCII, i.e. if the UTF-16 character was ASCII (<= 0x7f) the encoded character only takes up a single byte (and it's ASCII for a bonus).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 2
    Last Post: 08-13-2013, 10:17 AM
  2. Java charset equivalent to collation
    By mselva1587 in forum New To Java
    Replies: 1
    Last Post: 08-13-2013, 10:10 AM
  3. SQLite Connection with java
    By avirit1983 in forum NetBeans
    Replies: 2
    Last Post: 12-15-2010, 01:12 PM
  4. Trying to get JAVA to work with SQLite
    By mark8569 in forum JDBC
    Replies: 3
    Last Post: 04-25-2009, 02:42 AM
  5. JAVA and SQLite
    By mark8569 in forum Advanced Java
    Replies: 0
    Last Post: 03-20-2008, 07:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •