SQL Server : Query optimization - sql-server

Does SQL Server or SQL Server Compact Edition have built-in optimization for queries like this:
SELECT *
FROM
(SELECT *
FROM
(SELECT *
FROM
(SELECT * FROM table) ) )
I have nice looking recursive function and I don't want to change code
const string QUERY_SELECT = #"SELECT
*
FROM ( {0} ) AS RESULT
WHERE
{1} IN
(
SELECT
{1}
FROM ( {0} ) AS RESULT
GROUP BY
{1}
HAVING
COUNT( * ) > 1
)";
private string buildSelect( string[] fields )
{
string commandText = "SELECT * FROM [file]"; // ISSUE 1
foreach ( string field in fields )
{
this.buildSelect( ref commandText, field );
}
return commandText;
}
private void buildSelect( ref string commandText, string field )
{
commandText = String.Format
(
QUERY_SELECT,
commandText,
field
);
}
this.buildSelect( new string[] { "length", "extension" } );
The problem is brackets "()" :
( [file] ) AS RESULT ----> error
But I need brackets for
SELECT * FROM SELECT

Related

Android Studio: Database created, but no table

I am a newbie at programming so please bear with me (I am taking an online course).
I am at a point in the lesson where we were supposed to create a database in a java class and a table called "pets" would get created in the onCreate method of the main activity (called CatalogActivity).
At this point, I have already downloaded the app exactly in the form it is supposed to be (to make sure I haven't made a mistake before in my code), but when I run the app on the emulator, no table gets created.
The idea is, when I run the app, the database called shelter.db should get created and in it, the table called "pets". This table is defined in the PetDbHelper.java class.
When searching in Android Studio terminal, I see no tables in shelter.db. So I downloaded shelter.db to my PC and opened it with SQL browser - still no "pets" table. Looks like for some reason, the table doesn't get created :(
I don't know why as I seem to be following all the instructions to the point.
Does anyone have advice how to fix this, please?
Here is a link to the code: https://github.com/soralka/PetsApp_error
Thanks in advance!
Soralka
Here's the LOG when I use the CommonSQLiteUtilities class:
07-18 19:37:44.344 15867-15867/? I/le.android.pet: Not late-enabling -Xcheck:jni (already on)
07-18 19:37:44.416 15867-15867/? W/le.android.pet: Unexpected CPU variant for X86 using defaults: x86
07-18 19:37:44.662 15867-15867/com.example.android.pets I/le.android.pet: The ClassLoaderContext is a special shared library.
07-18 19:37:45.027 15867-15867/com.example.android.pets W/le.android.pet: JIT profile information will not be recorded: profile file does not exits.
07-18 19:37:45.051 15867-15867/com.example.android.pets I/chatty: uid=10083(com.example.android.pets) identical 10 lines
07-18 19:37:45.051 15867-15867/com.example.android.pets W/le.android.pet: JIT profile information will not be recorded: profile file does not exits.
07-18 19:37:45.137 15867-15867/com.example.android.pets I/InstantRun: starting instant run server: is main process
07-18 19:37:45.840 15867-15867/com.example.android.pets W/le.android.pet: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
07-18 19:37:45.850 15867-15867/com.example.android.pets W/le.android.pet: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
07-18 19:37:46.066 15867-15867/com.example.android.pets D/SQLITE_CSU: DatabaseList Row 1 Name=main File=/data/user/0/com.example.android.pets/databases/shelter.db
07-18 19:37:46.068 15867-15867/com.example.android.pets D/SQLITE_CSU: Database Version = 1
07-18 19:37:46.071 15867-15867/com.example.android.pets D/SQLITE_CSU: Table Name = android_metadata Created Using = CREATE TABLE android_metadata (locale TEXT)
07-18 19:37:46.073 15867-15867/com.example.android.pets D/SQLITE_CSU: Table = android_metadata ColumnName = locale ColumnType = TEXT Default Value = null PRIMARY KEY SEQUENCE = 0
07-18 19:37:46.074 15867-15867/com.example.android.pets D/SQLITE_CSU: Table Name = pets Created Using = CREATE TABLE pets (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, breed TEXT, gender INTEGER NOT NULL, weight INTEGER NOT NULL DEFAULT 0)
07-18 19:37:46.077 15867-15867/com.example.android.pets D/SQLITE_CSU: Table = pets ColumnName = _id ColumnType = INTEGER Default Value = null PRIMARY KEY SEQUENCE = 1
07-18 19:37:46.078 15867-15867/com.example.android.pets D/SQLITE_CSU: Table = pets ColumnName = name ColumnType = TEXT Default Value = null PRIMARY KEY SEQUENCE = 0
07-18 19:37:46.079 15867-15867/com.example.android.pets D/SQLITE_CSU: Table = pets ColumnName = breed ColumnType = TEXT Default Value = null PRIMARY KEY SEQUENCE = 0
Table = pets ColumnName = gender ColumnType = INTEGER Default Value = null PRIMARY KEY SEQUENCE = 0
Table = pets ColumnName = weight ColumnType = INTEGER Default Value = 0 PRIMARY KEY SEQUENCE = 0
07-18 19:37:46.080 15867-15867/com.example.android.pets D/SQLITE_CSU: Table Name = sqlite_sequence Created Using = CREATE TABLE sqlite_sequence(name,seq)
07-18 19:37:46.082 15867-15867/com.example.android.pets D/SQLITE_CSU: Table = sqlite_sequence ColumnName = name ColumnType = Default Value = null PRIMARY KEY SEQUENCE = 0
Table = sqlite_sequence ColumnName = seq ColumnType = Default Value = null PRIMARY KEY SEQUENCE = 0
07-18 19:37:46.134 15867-15867/com.example.android.pets D/OpenGLRenderer: Skia GL Pipeline
07-18 19:37:46.260 15867-15886/com.example.android.pets I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
07-18 19:37:46.262 15867-15886/com.example.android.pets I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
07-18 19:37:46.262 15867-15886/com.example.android.pets I/OpenGLRenderer: Initialized EGL, version 1.4
07-18 19:37:46.262 15867-15886/com.example.android.pets D/OpenGLRenderer: Swap behavior 1
07-18 19:37:46.262 15867-15886/com.example.android.pets W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
07-18 19:37:46.263 15867-15886/com.example.android.pets D/OpenGLRenderer: Swap behavior 0
07-18 19:37:46.284 15867-15886/com.example.android.pets D/EGL_emulation: eglCreateContext: 0xe4fbebc0: maj 3 min 0 rcv 3
07-18 19:37:46.347 15867-15886/com.example.android.pets D/EGL_emulation: eglMakeCurrent: 0xe4fbebc0: ver 3 0 (tinfo 0xe79c7930)
07-18 19:37:46.534 15867-15886/com.example.android.pets D/EGL_emulation: eglMakeCurrent: 0xe4fbebc0: ver 3 0 (tinfo 0xe79c7930)
Based upon your code I can see no issues.
You may wish to try deleting the App's data, or uninstalling the App adding the line below (or adding the CommonSQLiteUtilities class and adding the two lines that have been added for testing below) and then rerunning the App (this would overcome any issue you may have had that resulted in the database being created but not any tables, in which case the onCreate method would not run so any corrective changes (if any) may not have been applied).
Amending the CatalogActivity by adding the line :-
displayDatabaseInfo();
Results in the EditText being updated as expected (shows 0 rows in pets database).
If the underlying table didn't exist then if you added the line above, you'd experience an exception along the lines of :-
07-17 01:28:51.001 1321-1321/pets.pets E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{pets.pets/pets.pets.CatalogActivity}: android.database.sqlite.SQLiteException: no such table: notatable (code 1): , while compiling: SELECT * FROM notatable
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: no such table: notatable (code 1): , while compiling: SELECT * FROM notatable
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
at pets.pets.CatalogActivity.displayDatabaseInfo(CatalogActivity.java:40)
at pets.pets.CatalogActivity.onCreate(CatalogActivity.java:22)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
at android.app.ActivityThread.access$600(ActivityThread.java:130) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4745) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 
Note forced by changing the table name to notatable i.e.
Cursor cursor = db.rawQuery("SELECT * FROM " + "notatable", null);
Adding the CommonSQLiteUtilities class from Are there any methods that assist with resolving common SQLite issues?
and then adding the line :-
CommonSQLiteUtilities.logDatabaseInfo(mDbHelper.getWritableDatabase());
results in the log containing :-
07-17 01:36:51.191 1399-1399/? D/SQLITE_CSU: DatabaseList Row 1 Name=main File=/data/data/pets.pets/databases/shelter.db
Database Version = 1
Table Name = android_metadata Created Using = CREATE TABLE android_metadata (locale TEXT)
Table = android_metadata ColumnName = locale ColumnType = TEXT Default Value = null PRIMARY KEY SEQUENCE = 0
Table Name = pets Created Using = CREATE TABLE pets (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, breed TEXT, gender INTEGER NOT NULL, weight INTEGER NOT NULL DEFAULT 0)
Table = pets ColumnName = _id ColumnType = INTEGER Default Value = null PRIMARY KEY SEQUENCE = 1
Table = pets ColumnName = name ColumnType = TEXT Default Value = null PRIMARY KEY SEQUENCE = 0
Table = pets ColumnName = breed ColumnType = TEXT Default Value = null PRIMARY KEY SEQUENCE = 0
Table = pets ColumnName = gender ColumnType = INTEGER Default Value = null PRIMARY KEY SEQUENCE = 0
Table = pets ColumnName = weight ColumnType = INTEGER Default Value = 0 PRIMARY KEY SEQUENCE = 0
Table Name = sqlite_sequence Created Using = CREATE TABLE sqlite_sequence(name,seq)
Table = sqlite_sequence ColumnName = name ColumnType = Default Value = null PRIMARY KEY SEQUENCE = 0
Table = sqlite_sequence ColumnName = seq ColumnType = Default Value = null PRIMARY KEY SEQUENCE = 0
Further confirming that the table is created.
The testing above was done using the following code :-
CatalogActivity.java
public class CatalogActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_catalog);
//displayDatabaseInfo();
PetDbHelper mDbHelper = new PetDbHelper(this);
SQLiteDatabase db = mDbHelper.getReadableDatabase();
displayDatabaseInfo();
CommonSQLiteUtilities.logDatabaseInfo(mDbHelper.getWritableDatabase());
}
/**
* Temporary helper method to display information in the onscreen TextView about the state of
* the pets database.
*/
private void displayDatabaseInfo() {
// To access our database, we instantiate our subclass of SQLiteOpenHelper
// and pass the context, which is the current activity.
PetDbHelper mDbHelper = new PetDbHelper(this);
// Create and/or open a database to read from it
SQLiteDatabase db = mDbHelper.getReadableDatabase();
// Perform this raw SQL query "SELECT * FROM pets"
// to get a Cursor that contains all rows from the pets table.
Cursor cursor = db.rawQuery("SELECT * FROM " + PetContract.PetEntry.TABLE_NAME, null);
try {
// Display the number of rows in the Cursor (which reflects the number of rows in the
// pets table in the database).
TextView displayView = (TextView) findViewById(R.id.text_view_pet);
displayView.setText("Number of rows in pets database table: " + cursor.getCount());
} finally {
// Always close the cursor when you're done reading from it. This releases all its
// resources and makes it invalid.
cursor.close();
}
}
}
PetDbHelper.java
public class PetDbHelper extends SQLiteOpenHelper {
public static final String LOG_TAG = PetDbHelper.class.getSimpleName();
/**
* Name of the database file
*/
private static final String DATABASE_NAME = "shelter.db";
/**
* Database version. If you change the database schema, you must increment the database version.
*/
private static final int DATABASE_VERSION = 1;
/**
* Constructs a new instance of {#link PetDbHelper}.
*
* #param context of the app
*/
public PetDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
/**
* This is called when the database is created for the first time.
*/
#Override
public void onCreate(SQLiteDatabase db) {
// Create a String that contains the SQL statement to create the pets table
String SQL_CREATE_PETS_TABLE = "CREATE TABLE " + PetContract.PetEntry.TABLE_NAME + " ("
+ PetContract.PetEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ PetContract.PetEntry.COLUMN_PET_NAME + " TEXT NOT NULL, "
+ PetContract.PetEntry.COLUMN_PET_BREED + " TEXT, "
+ PetContract.PetEntry.COLUMN_PET_GENDER + " INTEGER NOT NULL, "
+ PetContract.PetEntry.COLUMN_PET_WEIGHT + " INTEGER NOT NULL DEFAULT 0);";
// Execute the SQL statement
db.execSQL(SQL_CREATE_PETS_TABLE);
}
/**
* This is called when the database needs to be upgraded.
*/
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// The database is still at version 1, so there's nothing to do be done here.
}
}
PetContract.java
public final class PetContract {
private PetContract() {}
public static final class PetEntry implements BaseColumns {
public static final String TABLE_NAME = "pets";
public static final String _ID = BaseColumns._ID;
public static final String COLUMN_PET_NAME = "name";
public static final String COLUMN_PET_BREED = "breed";
public static final String COLUMN_PET_GENDER = "gender";
public static final String COLUMN_PET_WEIGHT = "weight";
/* Possible values for gender. */
public static final int GENDER_UNKNOWN = 0;
public static final int GENDER_MALE = 1;
public static final int GENDER_FEMALE = 2;
}
}
CommonSQLiteUtilities.java
public class CommonSQLiteUtilities {
public static final boolean ERROR_CHECKING_ON = true;
public static final boolean ERROR_CHECKING_OFF = false;
// SQLite MASTER TABLE definitions
static final String SQLITE_MASTER = "sqlite_master";
static final String SM_TABLE_TYPE_COLUMN = "type";
static final String SM_NAME_COLUMN = "name";
static final String SM_TABLENAME_COLUMN = "tbl_name";
static final String SM_ROOTPAGE_COLUMN = "rootpage";
static final String SM_SQL_COLUMN = "sql";
static final String SM_TYPE_TABLE = "table";
static final String SM_TYPE_INDEX = "index";
static final String PRAGMA_STATEMENT = "PRAGMA ";
static final String PRAGMA_DATABASELIST = "database_list";
static final String PRAGMA_USERVERSION = "user_version";
static final String PRAGMA_ENCODING = "encoding";
static final String PRAGMA_FOREIGNKEYLIST = "foreign_key_list";
static final String PRAGMA_INDEXINFO = "index_info";
static final String PRAGMA_INDEXLIST = "index_list";
static final String PRAGMA_TABLEINFO = "table_info";
static final String PRAGMA_DBLIST_SEQ_COL = "seq";
static final String PRAGMA_DBLIST_NAME_COL = "name";
static final String PRAGMA_DBLIST_FILE_COL = "file";
static final String PRAGMA_TABLEINFO_CID_COL = "cid";
static final String PRAGMA_TABLEINFO_NAME_COl = "name";
static final String PRAGMA_TABLEINFO_TYPE_COL = "type";
static final String PRAGMA_TABLEINFO_NOTNULL_COL = "notnull";
static final String PRAGMA_TABLEINFO_DEFAULTVALUE_COL = "dflt_value";
static final String PRAGMA_TABLEINFO_PRIMARYKEY_COL = "pk";
static final String CSU_TAG = "SQLITE_CSU";
private CommonSQLiteUtilities() {}
/**
* Write Database information to the log;
* Information wrttien is:
* the database path, (will/should show connected databases)
* the version number (note! user version i.e. version coded in DBHelper),
* the tables in the database (includes android_metadata but not sqlite_master),
* the columns of the tables
* #param db The SQLite database to be interrogated
*/
public static void logDatabaseInfo(SQLiteDatabase db) {
// Issue PRAGMA database_list commnand
Cursor dblcsr = db.rawQuery(PRAGMA_STATEMENT + PRAGMA_DATABASELIST,null);
// Write databases to the log
while (dblcsr.moveToNext()) {
Log.d(CSU_TAG,"DatabaseList Row " + Integer.toString(dblcsr.getPosition() + 1) +
" Name=" + dblcsr.getString(dblcsr.getColumnIndex(PRAGMA_DBLIST_NAME_COL)) +
" File=" + dblcsr.getString(dblcsr.getColumnIndex(PRAGMA_DBLIST_FILE_COL))
);
}
dblcsr.close();
// Issue PRAGMA user_version to get the version and write to the log
//Note! to set user_version use execSQL not rawQuery
Cursor uvcsr = db.rawQuery(PRAGMA_STATEMENT + PRAGMA_USERVERSION,null);
while (uvcsr.moveToNext()) {
Log.d(CSU_TAG,"Database Version = " +
Integer.toString(uvcsr.getInt(uvcsr.getColumnIndex(PRAGMA_USERVERSION))));
}
uvcsr.close();
// Select all table entry rows from sqlite_master
Cursor tlcsr = db.rawQuery("SELECT * FROM " +
SQLITE_MASTER + " WHERE " +
SM_TABLE_TYPE_COLUMN + "='" + SM_TYPE_TABLE + "'"
,null);
// For each table write table information to the log
// (inner loop gets column info per table)
while (tlcsr.moveToNext()) {
String current_table = tlcsr.getString(tlcsr.getColumnIndex(SM_TABLENAME_COLUMN));
Log.d(CSU_TAG,
"Table Name = " + current_table +
" Created Using = " + tlcsr.getString(tlcsr.getColumnIndex(SM_SQL_COLUMN)),
null
);
// Issue PRAGMA tabel_info for the current table
Cursor ticsr = db.rawQuery(PRAGMA_STATEMENT + PRAGMA_TABLEINFO +
"(" + current_table + ")",
null
);
// Write column info (see headings below) to the log
while (ticsr.moveToNext()) {
Log.d(CSU_TAG,"Table = " +
current_table +
" ColumnName = " +
ticsr.getString(ticsr.getColumnIndex(PRAGMA_TABLEINFO_NAME_COl)) +
" ColumnType = " +
ticsr.getString(ticsr.getColumnIndex(PRAGMA_TABLEINFO_TYPE_COL)) +
" Default Value = " +
ticsr.getString(ticsr.getColumnIndex(PRAGMA_TABLEINFO_DEFAULTVALUE_COL)) +
" PRIMARY KEY SEQUENCE = " + Integer.toString(
ticsr.getInt(ticsr.getColumnIndex(PRAGMA_TABLEINFO_PRIMARYKEY_COL))
)
);
}
ticsr.close();
}
tlcsr.close();
}
/**
* Generic get all rows from an SQlite table,
* allowing the existence of the table to be checked and also
* allowing the ROWID to be added AS a supplied string
*
* #param db The SQLiteDatabase
* #param tablename The name of the table from which the
* returned cursor will be created from;
* Note!
* #param use_error_checking Whether ot not to try to detect errors
* currently just table doesn't exist,
* true to turn on, false to turn off
* ERROR_CHECKING_ON = true
* ERROR_CHECKING_OFF = false
* #param forceRowidAs If length of string passed is 1 or greater
* then a column, as an alias of ROWID, will be
* added to the cursor
* #return the extracted cursor, or in the case of the
* underlying table not existing an empty cursor
* with no columns
*/
public static Cursor getAllRowsFromTable(SQLiteDatabase db,
String tablename,
boolean use_error_checking,
String forceRowidAs) {
String[] columns = null;
// Tablename must be at least 1 character in length
if (tablename.length() < 1) {
Log.d(CSU_TAG,new Object(){}.getClass().getEnclosingMethod().getName() +
" is finishing as the provided tablename is less than 1 character in length"
);
return new MatrixCursor(new String[]{});
}
// If use_error_checking is true then check that the table exists
// in the sqlite_master table
if (use_error_checking) {
Cursor chkcsr = db.query(SQLITE_MASTER,null,
SM_TABLE_TYPE_COLUMN + "=? AND "
+ SM_TABLENAME_COLUMN + "=?",
new String[]{SM_TYPE_TABLE,tablename},
null,null,null
);
// Ooops table is not in the Database so return an empty
// column-less cursor
if (chkcsr.getCount() < 1) {
Log.d(CSU_TAG,"Table " + tablename +
" was not located in the SQLite Database Master Table."
);
// return empty cursor with no columns
return new MatrixCursor(new String[]{});
}
chkcsr.close();
}
// If forcing an alias of ROWID then user ROWID AS ???, *
if(forceRowidAs != null && forceRowidAs.length() > 0) {
columns = new String[]{"rowid AS " +forceRowidAs,"*"};
}
// Finally return the Cursor but trap any exceptions
try {
return db.query(tablename, columns, null, null, null, null, null);
} catch (Exception e) {
Log.d(CSU_TAG,"Exception encountered but trapped when querying table " + tablename +
" Message was: \n" + e.getMessage());
Log.d(CSU_TAG,"Stacktrace was:");
e.printStackTrace();
return new MatrixCursor(new String[]{});
}
}
/**
* Create and return a Cursor devoid of any rows and columns
* Not used, prehaps of very little use.
* #param db The Sqlite database in which the cursor is to be created
* #return The empty Cursor
*/
private static Cursor getEmptyColumnLessCursor(SQLiteDatabase db) {
return new MatrixCursor(new String[]{});
}
/**
* Write column names in the passed Cursor to the log
* #param csr The Cursor to be inspected.
*/
public static void logCursorColumns(Cursor csr) {
Log.d(CSU_TAG,
new Object(){}.getClass().getEnclosingMethod().getName() +
" invoked. Cursor has the following " +
Integer.toString(csr.getColumnCount())+
" columns.");
int position = 0;
for (String column: csr.getColumnNames()) {
position++;
Log.d(CSU_TAG,"Column Name " +
Integer.toString(position) +
" is "
+ column
);
}
}
/**
* Write the contents of the Cursor to the log
* #param csr The Cursor that is to be displayed in the log
*/
public static void logCursorData(Cursor csr) {
int columncount = csr.getColumnCount();
int rowcount = csr.getCount();
int csrpos = csr.getPosition(); //<<< added 20171016
Log.d(CSU_TAG,
new Object(){}.getClass().getEnclosingMethod().getName() +
" Cursor has " +
Integer.toString(rowcount) +
" rows with " +
Integer.toString(columncount) + " columns."
);
csr.moveToPosition(-1); //Ensure that all rows are retrieved <<< added 20171016
while (csr.moveToNext()) {
String unobtainable = "unobtainable!";
String logstr = "Information for row " + Integer.toString(csr.getPosition() + 1) + " offset=" + Integer.toString(csr.getPosition());
for (int i=0; i < columncount;i++) {
logstr = logstr + "\n\tFor Column " + csr.getColumnName(i);
switch (csr.getType(i)) {
case Cursor.FIELD_TYPE_NULL:
logstr = logstr + " Type is NULL";
break;
case Cursor.FIELD_TYPE_FLOAT:
logstr = logstr + "Type is FLOAT";
break;
case Cursor.FIELD_TYPE_INTEGER:
logstr = logstr + " Type is INTEGER";
break;
case Cursor.FIELD_TYPE_STRING:
logstr = logstr + " Type is STRING";
break;
case Cursor.FIELD_TYPE_BLOB:
logstr = logstr + " Type is BLOB";
break;
}
String strval_log = " value as String is ";
String lngval_log = " value as long is ";
String dblval_log = " value as double is ";
String blbval_log = "";
try {
strval_log = strval_log + csr.getString(i);
lngval_log = lngval_log + csr.getLong(i);
dblval_log = dblval_log + csr.getDouble(i);
} catch (Exception e) {
strval_log = strval_log + unobtainable;
lngval_log = lngval_log + unobtainable;
dblval_log = dblval_log + unobtainable;
try {
blbval_log = " value as blob is " +
getBytedata(csr.getBlob(i),24);
} catch (Exception e2) {
e2.printStackTrace();
}
}
logstr = logstr + strval_log + lngval_log + dblval_log + blbval_log;
}
Log.d(CSU_TAG,logstr);
}
csr.moveToPosition(csrpos); // restore cursor position <<< added 20171016
}
/**
* Return a hex string of the given byte array
* #param bytes The byte array to be converted to a hexadecimal string
* #param limit the maximum number of bytes;
* note returned string will be up to twice as long
* #return The byte array represented as a hexadecimal string
*/
private static String getBytedata(byte[] bytes, int limit) {
if (bytes.length < limit) {
return convertBytesToHex(bytes);
} else {
byte[] subset = new byte[limit];
System.arraycopy(bytes,0,subset,0,limit);
return convertBytesToHex(subset);
}
}
// HEX characters as a char array for use by convertBytesToHex
private final static char[] hexarray = "0123456789ABCDEF".toCharArray();
/**
* Return a hexadecimal string representation of the passed byte array
* #param bytes The byte array to be represented.
* #return The string representing the byte array as hexadecimal
*/
private static String convertBytesToHex(byte[] bytes) {
char[] hexstr = new char[bytes.length * 2];
for (int i=0; i < bytes.length; i++) {
int h = bytes[i] & 0xFF;
hexstr[i * 2] = hexarray[h >>> 4];
hexstr[i * 2 + 1] = hexarray[h & 0xF];
}
return new String(hexstr);
}
}
activity_catalog.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CatalogActivity">
<TextView
android:id="#+id/text_view_pet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
I found out what the main issue was: I was using an emulator with an API 28. Apparently, anything above API 23 is an issue when trying to access databases.
So I made a new emulator API 23 and now I don't have any issues with the permissions etc.
#MikeT - this seems to have been why I was struggling with getting the permissions.

Get default value of stored procedure parameter

I have created below stored procedure with default value:
CREATE PROCEDURE [dbo].[Sample1]
#OrderID INT = 10285
AS
SELECT ProductName, OrderID
FROM Products P, [Order Details] Od
WHERE Od.ProductID = P.ProductID
AND Od.OrderID = #OrderID
Tried to get default value (10285) of parameters using sys.parameters.
Select a.object_id, a.default_value
from sys.parameters a
inner join sys.types b on b.system_type_id = a.system_type_id
where Object_id = object_id('[dbo].[Sample1]')
But I got NULL as default_value, while I was expecting 10285 as default_value.
Is there any way to get default value?
It looks that Microsoft has neglected this topic and there is no trivial way to find parameters default values and even if a default value is present or not on a specific parameter:
As we all know, T-SQL stored procedure parameter defaults are not
stored in sys.parameters, all_parameters, and system_parameters. They
are also not exposed through sp_sproc_columns, sys.columns, or
sp_procedure_params_rowset.
Feedback from Microsoft:
As posted by Tibor Karaszi, BOL document that "SQL Server only
maintains default values for CLR objects in this catalog view;
therefore, this column has a value of 0 for Transact-SQL objects. To
view the default value of a parameter in a Transact-SQL object, query
the definition column of the sys.sql_modules catalog view, or use the
OBJECT_DEFINITION system function."
We dont store even the bit that indicating parameter is of default
value in Yukon.
I have tested the first code snippet in this answer and it seems to work for your simple example:
SELECT
data3.name
, [default_value] = REVERSE(RTRIM(SUBSTRING(
data3.rtoken
, CASE
WHEN CHARINDEX(N',', data3.rtoken) > 0
THEN CHARINDEX(N',', data3.rtoken) + 1
WHEN CHARINDEX(N')', data3.rtoken) > 0
THEN CHARINDEX(N')', data3.rtoken) + 1
ELSE 1
END
, LEN(data3.rtoken)
)))
FROM (
SELECT
data2.name
, rtoken = REVERSE(
SUBSTRING(ptoken
, CHARINDEX('=', ptoken, 1) + 1
, LEN(data2.ptoken))
)
FROM (
SELECT
data.name
, ptoken = SUBSTRING(
data.tokens
, token_pos + name_length + 1
, ISNULL(ABS(next_token_pos - token_pos - name_length - 1), LEN(data.tokens))
)
FROM (
SELECT
sm3.tokens
, p.name
, name_length = LEN(p.name)
, token_pos = CHARINDEX(p.name, sm3.tokens)
, next_token_pos = CHARINDEX(p2.name, sm3.tokens)
FROM (
SELECT
sm2.[object_id]
, sm2.[type]
, tokens = REVERSE(SUBSTRING(sm2.tokens, ISNULL(CHARINDEX('SA', sm2.tokens) + 2, 0), LEN(sm2.tokens)))
FROM (
SELECT
sm.[object_id]
, o.[type]
, tokens = REVERSE(SUBSTRING(
sm.[definition]
, CHARINDEX(o.name, sm.[definition]) + LEN(o.name) + 1
, ABS(CHARINDEX(N'AS', sm.[definition]))
)
)
FROM sys.sql_modules sm WITH (NOLOCK)
JOIN sys.objects o WITH (NOLOCK) ON sm.[object_id] = o.[object_id]
JOIN sys.schemas s WITH (NOLOCK) ON o.[schema_id] = s.[schema_id]
WHERE o.[type] = 'P '
AND s.name + '.' + o.name = 'dbo.Sample1'
) sm2
WHERE sm2.tokens LIKE '%=%'
) sm3
JOIN sys.parameters p WITH (NOLOCK) ON sm3.[object_id] = p.[object_id]
OUTER APPLY (
SELECT p2.name
FROM sys.parameters p2 WITH (NOLOCK)
WHERE p2.is_output = 0
AND sm3.[object_id] = p2.[object_id]
AND p.parameter_id + 1 = p2.parameter_id
) p2
WHERE p.is_output = 0
) data
) data2
WHERE data2.ptoken LIKE '%=%'
) data3
However, it is really ugly for a task that one expects to be easily queryable from system views.
I agree default stored procedure parameter values should be exposed via a SQL Server catalog view.
The T-SQL parsing method may work in many cases but is fragile. Consider using the TransactSQL ScriptDOM. Below is an example using a mix of PowerShell and C#. Not saying this will be perfect for all cases but it seems to process all the parameters I've thrown at it thusfar.
I used the Microsoft.SqlServer.TransactSql.ScriptDom.dll assembly from my SSMS install in this example but it can be downloaded from the NuGet Gallery.
try
{
Add-type -LiteralPath #("C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\ManagementStudio\Extensions\Application\Microsoft.SqlServer.TransactSql.ScriptDom.dll");
Add-type `
-ReferencedAssemblies #("C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\ManagementStudio\Extensions\Application\Microsoft.SqlServer.TransactSql.ScriptDom.dll") `
-TypeDefinition #"
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.TransactSql.ScriptDom;
using System.IO;
public static class ProcParser
{
public static List<StoredProcedureParameter> GetStoredProcedureParameters(string storedProcedureDefinition)
{
StringReader reader = new StringReader(storedProcedureDefinition);
var parser = new TSql140Parser(true);
IList<ParseError> errors;
TSqlFragment sqlFragment = parser.Parse(reader, out errors);
if (errors.Count > 0)
{
throw new Exception(`"Error parsing stored procedure definition`");
}
SQLVisitor sqlVisitor = new SQLVisitor();
sqlFragment.Accept(sqlVisitor);
return sqlVisitor.StoredProcedureParameters;
}
}
internal class SQLVisitor : TSqlFragmentVisitor
{
public List<StoredProcedureParameter> StoredProcedureParameters = new List<StoredProcedureParameter>();
public override void ExplicitVisit(ProcedureParameter node)
{
var p = StoredProcedureParameter.CreateProcedureParameter(node);
StoredProcedureParameters.Add(p);
}
}
public class StoredProcedureParameter
{
public string ParameterName;
public string ParameterType;
public string ParameterDirection = null;
public string DefaultParameterValue = null;
public static StoredProcedureParameter CreateProcedureParameter(ProcedureParameter node)
{
var param = new StoredProcedureParameter();
//parameter name
param.ParameterName = node.VariableName.Value;
//data type
switch (((ParameterizedDataTypeReference)node.DataType).Parameters.Count)
{
case 0:
if (node.DataType.Name.Identifiers.Count == 1)
{
param.ParameterType = node.DataType.Name.Identifiers[0].Value;
}
else
{
//schema-qualified type name
param.ParameterType = node.DataType.Name.Identifiers[0].Value + `".`" + node.DataType.Name.Identifiers[1].Value;
}
break;
case 1:
param.ParameterType = node.DataType.Name.Identifiers[0].Value + "(" + ((ParameterizedDataTypeReference)node.DataType).Parameters[0].Value + ")";
break;
case 2:
param.ParameterType = node.DataType.Name.Identifiers[0].Value + "(" + ((ParameterizedDataTypeReference)node.DataType).Parameters[0].Value + "," + ((ParameterizedDataTypeReference)node.DataType).Parameters[1].Value + ")";
break;
}
//default value
if (node.Value != null)
{
param.DefaultParameterValue = node.ScriptTokenStream[node.LastTokenIndex].Text;
}
//direction
if (node.Modifier == ParameterModifier.Output)
{
param.ParameterDirection = `"OUTPUT`";
}
else if (node.Modifier == ParameterModifier.ReadOnly)
{
param.ParameterDirection = `"READONLY`";
}
else
{
param.ParameterDirection = `"INPUT`";
}
return param;
}
public override string ToString()
{
var sb = new StringBuilder();
sb.Append(ParameterName);
sb.Append(`" `");
sb.Append(ParameterType);
if (DefaultParameterValue != null)
{
sb.Append(`" `");
sb.Append(DefaultParameterValue);
}
sb.Append(`" `");
sb.Append(ParameterDirection);
return sb.ToString();
}
}
"#
}
catch [System.Reflection.ReflectionTypeLoadException]
{
Write-Host "Message: $($_.Exception.Message)"
Write-Host "StackTrace: $($_.Exception.StackTrace)"
Write-Host "LoaderExceptions: $($_.Exception.LoaderExceptions)"
throw;
}
Function Get-ProcText($connectionString, $procName)
{
$connection = New-Object System.Data.SqlClient.SqlConnection($connectionString);
$connection.Open();
$command = New-Object System.Data.SqlClient.SqlCommand("SELECT definition FROM sys.sql_modules WHERE object_id = OBJECT_ID(#ProcName);", $connection);
$procNameParameter = $command.Parameters.Add((New-Object System.Data.SqlClient.SqlParameter("#ProcName", [System.Data.SqlDbType]::NVarChar, 261)));
$procNameParameter.Value = $procName;
$procText = $command.ExecuteScalar();
$connection.Close();
return $procText;
}
############
### main ###
############
try {
# get proc text definition from database
$procText = Get-ProcText `
-connectionString "Data Source=.;Initial Catalog=tempdb;Integrated Security=SSPI" `
-procName "dbo.testproc";
# parse parameters from proc text
$procParameters = [ProcParser]::GetStoredProcedureParameters($procText);
# display parameter values
foreach($procParameter in $procParameters)
{
Write-Host "ParameterName=$($procParameter.ParameterName)";
Write-Host "`tParameterType=$($procParameter.ParameterType)";
Write-Host "`tDefaultParameterValue=$($procParameter.DefaultParameterValue)";
Write-Host "`tParameterDirection=$($procParameter.ParameterDirection)";
}
}
catch {
throw;
}
I implemented Alexei's brilliant solution, but one of my variables, and a comment on a parameter both had the word 'class' in it and I couldn't for the life of me figure out why it broke. I finally realized that the word 'class' has 'AS' in it. So the key to avoiding that situation is to differentiate the AS keyword denoting the beginning of the body of the sp from random AS strings in the parameter section. I found a way that works for me, and might work for others. It relies on the fact that the AS keyword is on its own line prededed by CHAR(13) + CHAR(10) and succeeded by CHAR(13) + CHAR(10). So I modified these two lines:
, tokens = REVERSE(SUBSTRING(sm2.tokens, ISNULL(CHARINDEX(CHAR(10) + CHAR(13) + 'SA' + CHAR(10) + CHAR(13), sm2.tokens) + 2, 0), LEN(sm2.tokens)))
, tokens = REVERSE(SUBSTRING(
sm.[definition]
, CHARINDEX(o.name, sm.[definition]) + LEN(o.name) + 1
, ABS(CHARINDEX(CHAR(13) + CHAR(10) + N'AS' + CHAR(13) + CHAR(10), sm.[definition]))
)

FREETEXT search on a 'column_list' using hibernate CriteriaBuilder

is it possible to call Microsoft SQL Server's FREETEXT function on a column_list using the jpa CriteriaBuilder?
The SQL Server's FREETEXT function can be called natively on either a single column, or a list of columns:
https://learn.microsoft.com/en-us/sql/t-sql/queries/freetext-transact-sql
I can call FREETEXT on a single column after finding this:
https://stackoverflow.com/a/18534291/6659983
When I try to call FREETEXT on multiple columns, I can't find a way to get hibernate to put brackets around that column_list when I call FREETEXT.
I overrode ParamaterizedFunctionExpression.renderArguments() and finally managed to get the generated HQL (or some intermediate sort of QL) that looked like this:
select
generatedAlias0
from
package.model.entity as generatedAlias0
inner join generatedAlias0.categories as generatedAlias1
inner join generatedAlias1.unspsc as generatedAlias2
inner join generatedAlias0.buyer as generatedAlias3
inner join generatedAlias0.otherEntity as generatedAlias4
where
( function('FREETEXT', ( generatedAlias0.title, generatedAlias0.description, generatedAlias0.code, generatedAlias3.name, generatedAlias3.legalName, generatedAlias2.title ) , :param0) ) and ( generatedAlias0.state=:param1 )
and ( generatedAlias0.state<>:param2 )
and ( generatedAlias0.state<>:param3 )
and ( generatedAlias0.state<>:param4 )
order by
generatedAlias0.closingDate asc]
And I got this error:
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: function (FREETEXT) near line 1, column 319 [select generatedAlias0 from package.model.Entity as generatedAlias0 inner join generatedAlias0.categories as generatedAlias1 inner join generatedAlias1.unspsc as generatedAlias2 inner join generatedAlias0.buyer as generatedAlias3 inner join generatedAlias0.entityBoxes as generatedAlias4 where ( function('FREETEXT', ( generatedAlias0.title, generatedAlias0.description, generatedAlias0.code, generatedAlias3.name, generatedAlias3.legalName, generatedAlias2.title ) , :param0) ) and ( generatedAlias0.state=:param1 ) and ( generatedAlias0.state<>:param2 ) and ( generatedAlias0.state<>:param3 ) and ( generatedAlias0.state<>:param4 ) order by generatedAlias0.closingDate asc]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:131)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:663)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:3318)
at org.hibernate.query.criteria.internal.CriteriaQueryImpl$1.buildCompiledQuery(CriteriaQueryImpl.java:318)
at org.hibernate.query.criteria.internal.compile.CriteriaCompiler.compile(CriteriaCompiler.java:127)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:3611)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:203)
at package.entity.dao.EntityDaoImpl.search(EntityDaoImpl.java:241)
Here is my effort to extend ParameterizedFunctionExpression:
/**
* Overrides the {#link ParameterizedFunctionExpression.renderArguments()} function
* so that the FREETEXT search can be created properly.
*/
private class FreeTextExpression extends ParameterizedFunctionExpression<Boolean> implements Predicate {
/**
* Eclipse Generated UID.
*/
private static final long serialVersionUID = -1219262363097942038L;
private List<Expression<?>> argumentExpressions;
/**
* Constructs a FreeTextExpression
* #param criteriaBuilder
* #param returnType
* #param functionName
* #param argumentExpressions
*/
public FreeTextExpression(
CriteriaBuilderImpl criteriaBuilder,
String functionName,
List<Expression<?>> argumentExpressions) {
super( criteriaBuilder, (Class<Boolean>) Boolean.class , functionName , argumentExpressions);
this.argumentExpressions = argumentExpressions;
}
/**
* Places all arguments except the first within brackets in order to define a MSSQL 'column_list'.
*/
#Override
protected void renderArguments(StringBuilder buffer, RenderingContext renderingContext) {
String sep = "";
buffer.append(" ( ");
for (int i = 0; i < argumentExpressions.size() - 1 ; i ++) {
buffer.append( sep ).append( ( (Renderable) argumentExpressions.get(i) ).render( renderingContext ) );
sep = ", ";
}
buffer.append(" ) ");
buffer.append( sep ).append( ( (Renderable) argumentExpressions.get(argumentExpressions.size() - 1) ).render( renderingContext ) );
}
#Override
public BooleanOperator getOperator() {
return Predicate.BooleanOperator.AND;
}
#Override
public boolean isNegated() {
return false;
}
#Override
public List<Expression<Boolean>> getExpressions() {
return Arrays.asList(this);
}
#Override
public Predicate not() {
return null;
}
}
Here is how I use it:
List<Expression<?>> arguments = Arrays.asList(
entity.<String>get(Entity_.title),
entity.<String>get(Entity_.description),
entity.<String>get(Entity_.code),
buyer.<String>get(Business_.name),
buyer.<String>get(Business_.legalName),
unspsc.<String>get(Unspsc_.title),
(Expression<String>) keywords
);
Expression<Boolean> freeTextExpression = new FreeTextExpression(
(CriteriaBuilderImpl) builder, "FREETEXT", arguments);
criteria.add((Predicate) freeTextExpression);
Oops, I realised I need to register a function like this, but for FREETEXT:
registerFunction("CONTAINS", new SQLFunctionTemplate(StandardBasicTypes.BOOLEAN, "CONTAINS(?1, ?2) AND 1"));
Hibernate + MSSQL + Fulltext Search via Contains SQLFunctionTemplate
Will post code once I've worked it out, unless someone can beat me to it!
please try this :
select
generatedAlias0
from
package.model.entity as generatedAlias0
inner join generatedAlias0.categories as generatedAlias1
inner join generatedAlias1.unspsc as generatedAlias2
inner join generatedAlias0.buyer as generatedAlias3
inner join generatedAlias0.otherEntity as generatedAlias4
where
FREETEXT( generatedAlias0,#param0) ) and ( generatedAlias0,#param1 )
and ( generatedAlias0.state<>:param2 )
and ( generatedAlias0.state<>:param3 )
and ( generatedAlias0.state<>:param4 )
order by
generatedAlias0.closingDate asc]

How to Insert records into a table in Unity using SQLite?

I'm trying to add a record to my table using string variables for VarChar columns. I want to insert the values I give it under the next incremented ID. However, I keep getting the SQL error that "no such column exists" when I try and run the code. There is no elaboration on where the error occurs, so I'm sorta stuck. So how can I insert records into a table?
Here's my code
using UnityEngine;
using Mono.Data.Sqlite;
using System.Data;
using UnityEngine.UI;
using System.Collections;
public class AddQuery : MonoBehaviour {
public Text cipher;
public Text initialMessage;
public Text encryptedMessage;
private string _constr = "URI=file:previousMessages.db";
private IDbConnection _dbc;
private IDbCommand _dbcm;
private IDataReader _dbr;
public void AddSQL() {
string _cipher = cipher.text;
string _initialMessage = initialMessage.text;
string _encryptedMessage = encryptedMessage.text;
_dbc = new SqliteConnection(_constr);
_dbc.Open();
_dbcm = _dbc.CreateCommand();
_dbcm.CommandText = "CREATE TABLE IF NOT EXISTS previousMessages (ID INTEGER NOT NULL PRIMARY KEY , Cipher VARCHAR(5000) NOT NULL, InitialMessage VARCHAR(5000) NOT NULL,EncryptedMessage TEXT NOT NULL)";
_dbr = _dbcm.ExecuteReader();
_dbr.Close();
_dbcm.CommandText = "INSERT INTO previousMessages ( Cipher, InitialMessage, EncryptedMessage) VALUES ( "+_cipher+", "+_initialMessage+", "+_encryptedMessage+")";
_dbr = _dbcm.ExecuteReader();
_dbr.Close();
_dbcm.CommandText = "SELECT FROM previousMessages (Cipher, InitialMessage, EncryptedMessage) VALUES ( " + _cipher + ", " + _initialMessage + ", " + _encryptedMessage + ")";
_dbr = _dbcm.ExecuteReader();
while (_dbr.Read())
{
Debug.Log("Cipher: " + _dbr["Cipher"] + "\t Initial: " + _dbr["InitialMessage"]);
}
}
}
If you follow OOP you're life will be simpler. I mean you'd rather let a method do only one thing; or if it HAS TO do many things than make it a list of calls to other methods which do only 1 thing each. That being said your AddSQL() breaks down to:
public void AddSQL() {
string _cipher = cipher.text;
string _initialMessage = initialMessage.text;
string _encryptedMessage = encryptedMessage.text;
OpenConnection();
ExecuteCreateCommand();
InsertCommand(_cipher, _initialMessage, _encryptedMessage);
ReadStuff(_cipher, _initialMessage, _encryptedMessage);
DebugRead();
}
void OpenConnection(){
_dbc = new SqliteConnection(_constr);
_dbc.Open();
}
void ExecuteCreateCommand(){
_dbcm = _dbc.CreateCommand();
_dbcm.CommandText = "CREATE TABLE IF NOT EXISTS previousMessages (ID INTEGER NOT NULL PRIMARY KEY , Cipher VARCHAR(5000) NOT NULL, InitialMessage VARCHAR(5000) NOT NULL,EncryptedMessage TEXT NOT NULL)";
_dbr = _dbcm.ExecuteReader();
_dbr.Close();
}
void InsertCommand(string _cipher, string _initialMessage, string _encryptedMessage){
_dbcm.CommandText = "INSERT INTO previousMessages ( Cipher, InitialMessage, EncryptedMessage) VALUES ( "+_cipher+", "+_initialMessage+", "+_encryptedMessage+")";
_dbr = _dbcm.ExecuteReader();
_dbr.Close();
}
void ReadStuff(string _cipher, string _initialMessage, string _encryptedMessage){
_dbcm.CommandText = "SELECT FROM previousMessages (Cipher, InitialMessage, EncryptedMessage) VALUES ( " + _cipher + ", " + _initialMessage + ", " + _encryptedMessage + ")";
_dbr = _dbcm.ExecuteReader();
}
void DebugRead(){
while (_dbr.Read())
{
Debug.Log("Cipher: " + _dbr["Cipher"] + "\t Initial: " + _dbr["InitialMessage"]);
}
}
Now in AddSQL() comment all the method calls and uncomment one by one to see which query throws an exception.

auto increment ID in H2 database

Is there a way to have an auto_incrementing BIGINT ID for a table.
It can be defined like so
id bigint auto_increment
but that has no effect (it does not increment automatically).
I would like to insert all fields but the ID field - the ID field should be provided by the DBMS.
Or do I need to call something to increment the ID counter?
It works for me. JDBC URL: jdbc:h2:~/temp/test2
drop table test;
create table test(id bigint auto_increment, name varchar(255));
insert into test(name) values('hello');
insert into test(name) values('world');
select * from test;
result:
ID NAME
1 hello
2 world
IDENTITY
The modern approach uses the IDENTITY type, for automatically generating an incrementing 64-bit long integer.
This single-word syntax used in H2 is an abbreviated variation of GENERATED … AS IDENTITY defined in the SQL:2003 standard. See summary in PDF document SQL:2003 Has Been Published. Other databases are implementing this, such as Postgres.
CREATE TABLE event_
(
pkey_ IDENTITY NOT NULL PRIMARY KEY , -- ⬅ `identity` = auto-incrementing long integer.
name_ VARCHAR NOT NULL ,
start_ TIMESTAMP WITH TIME ZONE NOT NULL ,
duration_ VARCHAR NOT NULL
)
;
Example usage. No need to pass a value for our pkey column value as it is being automatically generated by H2.
INSERT INTO event_ ( name_ , start_ , stop_ )
VALUES ( ? , ? , ? )
;
And Java.
ZoneId z = ZoneId.of( "America/Montreal" ) ;
OffsetDateTime start = ZonedDateTime.of( 2021 , Month.JANUARY , 23 , 19 , 0 , 0 , 0 , z ).toOffsetDateTime() ;
Duration duration = Duration.ofHours( 2 ) ;
myPreparedStatement.setString( 1 , "Java User Group" ) ;
myPreparedStatement.setObject( 2 , start ) ;
myPreparedStatement.setString( 3 , duration.toString() ) ;
Returning generated keys
Statement.RETURN_GENERATED_KEYS
You can capture the value generated during that insert command execution. Two steps are needed. First, pass the flag Statement.RETURN_GENERATED_KEYS when getting your prepared statement.
PreparedStatement pstmt = conn.prepareStatement( sql , Statement.RETURN_GENERATED_KEYS ) ;
Statement::getGeneratedKeys
Second step is to call Statement::getGeneratedKeys after executing your prepared statement. You get a ResultSet whose rows are the identifiers generated for the created row(s).
Example app
Here is an entire example app. Running on Java 14 with Text Blocks preview feature enabled for fun. Using H2 version 1.4.200.
package work.basil.example;
import org.h2.jdbcx.JdbcDataSource;
import java.sql.*;
import java.time.*;
import java.util.Objects;
public class H2ExampleIdentity
{
public static void main ( String[] args )
{
H2ExampleIdentity app = new H2ExampleIdentity();
app.doIt();
}
private void doIt ( )
{
JdbcDataSource dataSource = Objects.requireNonNull( new JdbcDataSource() ); // Implementation of `DataSource` bundled with H2.
dataSource.setURL( "jdbc:h2:mem:h2_identity_example_db;DB_CLOSE_DELAY=-1" ); // Set `DB_CLOSE_DELAY` to `-1` to keep in-memory database in existence after connection closes.
dataSource.setUser( "scott" );
dataSource.setPassword( "tiger" );
String sql = null;
try (
Connection conn = dataSource.getConnection() ;
)
{
sql = """
CREATE TABLE event_
(
id_ IDENTITY NOT NULL PRIMARY KEY, -- ⬅ `identity` = auto-incrementing integer number.
title_ VARCHAR NOT NULL ,
start_ TIMESTAMP WITHOUT TIME ZONE NOT NULL ,
duration_ VARCHAR NOT NULL
)
;
""";
System.out.println( "sql: \n" + sql );
try ( Statement stmt = conn.createStatement() ; )
{
stmt.execute( sql );
}
// Insert row.
sql = """
INSERT INTO event_ ( title_ , start_ , duration_ )
VALUES ( ? , ? , ? )
;
""";
try (
PreparedStatement pstmt = conn.prepareStatement( sql , Statement.RETURN_GENERATED_KEYS ) ;
)
{
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime start = ZonedDateTime.of( 2021 , 1 , 23 , 19 , 0 , 0 , 0 , z );
Duration duration = Duration.ofHours( 2 );
pstmt.setString( 1 , "Java User Group" );
pstmt.setObject( 2 , start.toOffsetDateTime() );
pstmt.setString( 3 , duration.toString() );
pstmt.executeUpdate();
try (
ResultSet rs = pstmt.getGeneratedKeys() ;
)
{
while ( rs.next() )
{
int id = rs.getInt( 1 );
System.out.println( "generated key: " + id );
}
}
}
// Query all.
sql = "SELECT * FROM event_ ;";
try (
Statement stmt = conn.createStatement() ;
ResultSet rs = stmt.executeQuery( sql ) ;
)
{
while ( rs.next() )
{
//Retrieve by column name
int id = rs.getInt( "id_" );
String title = rs.getString( "title_" );
OffsetDateTime odt = rs.getObject( "start_" , OffsetDateTime.class ); // Ditto, pass class for type-safety.
Instant instant = odt.toInstant(); // If you want to see the moment in UTC.
Duration duration = Duration.parse( rs.getString( "duration_" ) );
//Display values
ZoneId z = ZoneId.of( "America/Montreal" );
System.out.println( "id_" + id + " | start_: " + odt + " | duration: " + duration + " ➙ running from: " + odt.atZoneSameInstant( z ) + " to: " + odt.plus( duration ).atZoneSameInstant( z ) );
}
}
}
catch ( SQLException e )
{
e.printStackTrace();
}
}
}
Next, see results when run.
Instant, OffsetDateTime, & ZonedDateTime
At the time of this execution, my JVM’s current default time zone is America/Los_Angeles. At the point in time of the stored moment (January 23, 2021 at 7 PM in Québec), the zone America/Los_Angeles had an offset-from-UTC of eight hours behind. So the OffsetDateTime object returned by the H2 JDBC driver is set to an offset of -08:00. This is a distraction really, so in real work I would immediately convert that OffsetDateTime to either an Instant for UTC or ZonedDateTime for a specific time zone I had in mind. Be clear in understanding that the Instant, OffsetDateTime, and ZonedDateTime objects would all represent the same simultaneous moment, the same point on the timeline. Each views that same moment through a different wall-clock time. Imagine 3 people in California, Québec, and Iceland (whose zone is UTC, an offset of zero) all talking in a conference call end they each looked up at the clock on their respective wall at the same coincidental moment.
generated key: 1
id_1 | start_: 2021-01-23T16:00-08:00 | duration: PT2H ➙ running from: 2021-01-23T19:00-05:00[America/Montreal] to: 2021-01-23T21:00-05:00[America/Montreal]
By the way, in real work on an app booking future appointments, we would use a different data type in Java and in the database.
We would have used LocalDateTime and ZoneId in Java. In the database, we would have used a data type akin to the SQL standard type TIMESTAMP WITHOUT TIME ZONE with a second column for the name of the intended time zone. When retrieving values from the database to build an scheduling calendar, we would apply the time zone to the stored date-time to get a ZonedDateTime object. This would allow us to book appointments for a certain time-of-day regardless of changes to the offset-from-UTC made by the politicians in that jurisdiction.
Very simple:
id int auto_increment primary key
H2 will create Sequence object automatically
You can also use default:
create table if not exists my(id int auto_increment primary key,s text);
insert into my values(default,'foo');
id bigint(size) zerofill not null auto_increment,

Resources