Package totalcross.db.sqlite
Class SQLiteUtil
- java.lang.Object
-
- totalcross.db.sqlite.SQLiteUtil
-
public class SQLiteUtil extends java.lang.ObjectUtility class to make convertion to SQLite easier. Important rules about date and time in SQLite.- DATE type: must be in the form: YYYY-MM-DD
- TIME type: must be in the form: YYYY-MM-DD HH:MM:SS.MMM
- If borndate is a DATE, the comparison will succeed.
- If borndate is a TIME, the comparison will fail. To make it work, use "select borndate from test where borndate between '2014-12-10 00:00:00.000' and '2014-12-14 00:00:00.000'".
- See Also:
Date.getSQLString(),Time.getSQLString()
-
-
Field Summary
Fields Modifier and Type Field Description java.lang.StringfullPathintvectorInitialSize
-
Constructor Summary
Constructors Constructor Description SQLiteUtil()Open a connection at memorySQLiteUtil(java.lang.String table)Open a connection at the given tableSQLiteUtil(java.lang.String path, java.lang.String table)Open a connection at the given path and tableSQLiteUtil(Connection con)Use the given connection in all operations
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()voidclose(ResultSet rs)Connectioncon()Returns the connecton with the parameters passed in the constructorResultSetexecuteQuery(java.lang.String s)static booleanexistsIndex(java.lang.String tableName, java.lang.String indexName, Connection connection)Check if the index with the name exists in this tablevoidfinishTransaction()static java.lang.StringfixQuote(java.lang.String s, boolean toSqlite)Handles single quote when inserting or retrieving data from Sqlite.static DatefromSqlDate(java.lang.String sqldate)Changes a date in format 2014-02-19 00:00:00:000 to a totalcross.util.Date.intgetColCount(ResultSet rs)intgetColumnType(ResultSetMetaData md, int col)SQLite has a problem (not sure if its a bug) where it returns DATE for both DATE and DATETIME types, so this method returns the correct correspondence: DATE for date and TIME for DATETIME (remember that in TotalCross, a Time object also contains the date).intgetInt(java.lang.String sql)intgetInt(ResultSet rs)intgetRowCount(java.lang.String table)intgetShort(java.lang.String sql)intgetShort(ResultSet rs)java.lang.StringgetString(java.lang.String sql)java.lang.String[][]getStrings(java.lang.String sql)java.lang.String[][]getStrings(ResultSet rs)java.lang.String[][]getStrings(ResultSet rs, Vector v)java.lang.String[][]getStrings(ResultSet rs, Vector v, int[] decimalPlaces)java.lang.String[]getStrings1(java.lang.String sql)java.lang.String[]getStrings1(ResultSet rs)booleanisNotEmpty(java.lang.String sql)booleanisNotEmpty(ResultSet rs)java.lang.String[]listAllTables()PreparedStatementprepareStatement(java.lang.String sql)voidrollback()voidshrinkDB()Rebuild and shrink the entire database.voidstartTransaction()booleantableExists(java.lang.String tab)
-
-
-
Constructor Detail
-
SQLiteUtil
public SQLiteUtil(java.lang.String table) throws java.sql.SQLExceptionOpen a connection at the given table- Throws:
java.sql.SQLException
-
SQLiteUtil
public SQLiteUtil(java.lang.String path, java.lang.String table) throws java.sql.SQLExceptionOpen a connection at the given path and table- Throws:
java.sql.SQLException
-
SQLiteUtil
public SQLiteUtil() throws java.sql.SQLExceptionOpen a connection at memory- Throws:
java.sql.SQLException
-
SQLiteUtil
public SQLiteUtil(Connection con)
Use the given connection in all operations
-
-
Method Detail
-
con
public Connection con() throws java.sql.SQLException
Returns the connecton with the parameters passed in the constructor- Throws:
java.sql.SQLException
-
close
public void close()
-
tableExists
public boolean tableExists(java.lang.String tab) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
isNotEmpty
public boolean isNotEmpty(java.lang.String sql)
-
isNotEmpty
public boolean isNotEmpty(ResultSet rs)
-
getColCount
public int getColCount(ResultSet rs) throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
executeQuery
public ResultSet executeQuery(java.lang.String s) throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
close
public void close(ResultSet rs)
-
getStrings1
public java.lang.String[] getStrings1(java.lang.String sql)
-
getStrings1
public java.lang.String[] getStrings1(ResultSet rs)
-
getColumnType
public int getColumnType(ResultSetMetaData md, int col) throws java.sql.SQLException
SQLite has a problem (not sure if its a bug) where it returns DATE for both DATE and DATETIME types, so this method returns the correct correspondence: DATE for date and TIME for DATETIME (remember that in TotalCross, a Time object also contains the date).- Parameters:
md- The ResultSetMetaData obtained with ResultSet.getMetaData.col- The column, starting from 1.- Throws:
java.sql.SQLException
-
getStrings
public java.lang.String[][] getStrings(ResultSet rs, Vector v) throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
getStrings
public java.lang.String[][] getStrings(ResultSet rs, Vector v, int[] decimalPlaces) throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
getStrings
public java.lang.String[][] getStrings(ResultSet rs) throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
getStrings
public java.lang.String[][] getStrings(java.lang.String sql) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
getString
public java.lang.String getString(java.lang.String sql) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
getInt
public int getInt(java.lang.String sql) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
getInt
public int getInt(ResultSet rs) throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
getShort
public int getShort(java.lang.String sql) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
getShort
public int getShort(ResultSet rs) throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
getRowCount
public int getRowCount(java.lang.String table)
-
startTransaction
public void startTransaction() throws java.sql.SQLException- Throws:
java.sql.SQLException
-
finishTransaction
public void finishTransaction() throws java.sql.SQLException- Throws:
java.sql.SQLException
-
rollback
public void rollback() throws java.sql.SQLException- Throws:
java.sql.SQLException
-
prepareStatement
public PreparedStatement prepareStatement(java.lang.String sql) throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
listAllTables
public java.lang.String[] listAllTables()
-
fixQuote
public static java.lang.String fixQuote(java.lang.String s, boolean toSqlite)Handles single quote when inserting or retrieving data from Sqlite. Example:String s = SQLiteUtil.fixQuote("'",true); // returns '' String s = SQLiteUtil.fixQuote("''",false); // returns '
-
fromSqlDate
public static Date fromSqlDate(java.lang.String sqldate) throws InvalidDateException
Changes a date in format 2014-02-19 00:00:00:000 to a totalcross.util.Date.- Throws:
InvalidDateException
-
shrinkDB
public void shrinkDB() throws java.sql.SQLExceptionRebuild and shrink the entire database.- Throws:
java.sql.SQLException
-
existsIndex
public static final boolean existsIndex(java.lang.String tableName, java.lang.String indexName, Connection connection) throws java.sql.SQLExceptionCheck if the index with the name exists in this table- Parameters:
tableName- to checkindexName- to checkconnection- with the database- Returns:
- true if the index exists, otherwise false;
- Throws:
java.sql.SQLException
-
-