I am trying to write my first Kotlin app using SQLite and here is the problematic part:
override fun onCreate(p0: SQLiteDatabase?) {
p0!!.execSQL("CREATE TABLE IF NOT EXISTS $CAR_TABLE(" +
"$CAR_ID INTEGER PRIMARY KEY AUTOINCREMENT, $CAR_NAME TEXT," +
"$CAR_COLOR TEXT, $CAR_MODEL INTEGER)")
I am facing this error message:
'(', ')', or comma expected, got 'INTEGER'
interestingly enough if I delete the last part like this:
override fun onCreate(p0: SQLiteDatabase?) {
p0!!.execSQL("CREATE TABLE IF NOT EXISTS $CAR_TABLE(" +
"$CAR_ID INTEGER PRIMARY KEY AUTOINCREMENT, $CAR_NAME TEXT," +
"$CAR_COLOR TEXT)")
there won't be any error messages. Could you help me with it please?
Android Studio "Bumblebee".
Thank you in advance
Related
I am creating IRI with [name] as value. Though IRI is getting created, when I call getShortForm() on that IRI, I am not getting [name] but I am getting string after the first '/'.
Example :
IRI.create("http://example.com/myPage" + "#" + "[name]"). When I call IRI.getShortForm() I am getting "myPage#[name]" instead of just [name].
But when I create IRI.create("http://example.com/myPage" + "#" + "name") like this and then call getShortForm(), I am getting name.
I have this query below that is working fine on MySql and Oracle but fail on SQLServer:
#Query("SELECT ca FROM ContextAccess ca " + "LEFT JOIN UserContextAccess uca on ca.id = uca.id " +
"LEFT JOIN RegularUserInfo rui on rui.user = uca.user " +
"WHERE ca.context = :context and (COALESCE(:roles, null) is null or ca.role in (:roles)) " +
"AND (:name is null or ca.accessType='GROUP' or CONCAT(rui.firstName,' ',rui.lastName) like CONCAT(:name,'%') " +
"or CONCAT(rui.lastName,' ',rui.firstName) like CONCAT(:name,'%'))")
I have this stacktrace:
com.microsoft.sqlserver.jdbc.SQLServerException: The data types varbinary and varchar are incompatible in the add operator.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:262)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1624)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:594)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:524)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7194)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2979)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:248)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:223)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:446)
the problem come from CONCAT(:name,'%')
I can solve it by using '+' operator like this :name + '%' but after it's not working on Oracle
Do you have an idea to solve this problem ?
Thanks,
You can use CONCAT(:name,\"%\")
The double-quote character has to be escaped with a backslash in a Java string literal.
Problem:
We try to run your update script in h2 for testing.
With every version we have a update.sql to run the updates on the DB.
But we get a problem when trying to delete a constraint (foreign key):
Error:
Exception in thread "main" org.h2.jdbc.JdbcSQLException: Index "FP_ACL_PERMISSION_UNQ" belongs to constraint "CONSTRAINT_31B"; SQL statement:
ALTER TABLE FP_ACL_PERMISSION DROP INDEX FP_ACL_PERMISSION_UNQ; [90085-191]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.command.ddl.DropIndex.update(DropIndex.java:63)
at org.h2.command.CommandContainer.update(CommandContainer.java:98)
at org.h2.command.Command.executeUpdate(Command.java:258)
at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:184)
at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:158)
at com.rbs.mib.portal.HelloWorld.main(HelloWorld.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Code to reproduce the problem:
public class ReproduceH2Problem{
public static void main(String... args) throws Exception {
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:~/test;MODE=ORACLE", "sa", "sa");
Statement stat = conn.createStatement();
stat.execute("DROP ALL OBJECTS");
// init v1.0.0
stat.execute("CREATE TABLE FP_ACL" +
"(" +
" ACL_ID INTEGER NOT NULL," +
" REPORT_KEY VARCHAR2(100) NOT NULL," +
" PARENT_ACL_ID INTEGER" +
");");
stat.execute("CREATE TABLE FP_ACL_PERMISSION" +
"(" +
" PERMISSION_ID INTEGER NOT NULL," +
" ACL_ID INTEGER NOT NULL," +
" NAME VARCHAR2(100)" +
");");
stat.execute("CREATE UNIQUE INDEX FP_ACL_PERMISSION_UNQ ON FP_ACL_PERMISSION" +
"(ACL_ID, NAME);");
stat.execute("ALTER TABLE FP_ACL_PERMISSION ADD " +
"CHECK (ACL_ID IS NOT NULL);");
stat.execute("ALTER TABLE FP_ACL_PERMISSION ADD " +
"PRIMARY KEY " +
"(PERMISSION_ID);");
stat.execute("ALTER TABLE FP_ACL_PERMISSION ADD " +
"CONSTRAINT FP_ACL_PERMISSION_UNQ " +
"UNIQUE (ACL_ID, NAME);");
// !! this seems to be the "bad guy" !!
stat.execute("ALTER TABLE FP_ACL_PERMISSION ADD " +
"FOREIGN KEY (ACL_ID) " +
"REFERENCES FP_ACL (ACL_ID);");
//update v1.0.1
stat.execute("ALTER TABLE FP_ACL_PERMISSION DROP CONSTRAINT FP_ACL_PERMISSION_UNQ;");
//here the problems start
stat.execute("ALTER TABLE FP_ACL_PERMISSION DROP INDEX FP_ACL_PERMISSION_UNQ;");
stat.execute("ALTER TABLE FP_ACL_PERMISSION DROP COLUMN ACL_ID;");
stat.execute("ALTER TABLE FP_ACL_PERMISSION ADD CONSTRAINT FP_ACL_PERMISSION_UNQ UNIQUE (NAME);");
stat.close();
conn.close();
}
}
I had to do a workaround by adding meta commands in the sql scripts as
cannot rename the constraints (h2 does not support)
direct subselect does not work
alter table TABLE_NAME drop constraint
(select unique_index_name
from information_schema.constraints
where table_name='TABLE_NAME' and CONSTRAINT_TYPE='REFERENTIAL' and COLUMN_LIST= ='SHORT_ID')
Workaround:
Meta Command in update.sql script:
--H2-DROP-REFERENCE TABLE::FP_ACL_PERMISSION:: COLUMN::ACL_ID::
ALTER TABLE FP_ACL_PERMISSION DROP INDEX FP_ACL_PERMISSION_UNQ;
Filter meta command in groovy test:
public static enum H2_META_COMMAND {
DROP_REFERENCE("--H2-DROP-REFERENCE")
private String command;
H2_META_COMMAND(String command){
this.command = command
}
String getCommand(){
return command;
}
}
and
/**
* This checks for custom H2 Meta commands to close oracle compatibility gap
* #param sqlCommand a single command
*/
public static void executeH2MetaCommands(String sqlSingleCommand, Statement statement){
if(sqlCommand.contains(H2_META_COMMAND.DROP_REFERENCE.getCommand())){
int tableStart = sqlCommand.indexOf("TABLE::", sqlCommand.indexOf(H2_META_COMMAND.DROP_REFERENCE.getCommand()) + H2_META_COMMAND.DROP_REFERENCE.getCommand().size() ) + 7
String table = sqlCommand.substring(tableStart, sqlCommand.indexOf("::",tableStart))
int colStart = sqlCommand.indexOf("COLUMN::") + 8
String col = sqlCommand.substring(colStart, sqlCommand.indexOf("::",colStart))
ResultSet rs = statement.executeQuery("select CONSTRAINT_NAME from information_schema.constraints where table_name='"+ table+"' and CONSTRAINT_TYPE='REFERENTIAL' and COLUMN_LIST= '"+col+"'")
rs.next()
String constraintName = rs.getString(1)
rs.close()
statement.execute("ALTER TABLE FP_ACL_PERMISSION DROP constraint " + constraintName)
}
}
I am creating an SQLite database.
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " ("
+ KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_NAME + " TEXT NOT NULL, "
+ KEY_WORKED + " INTEGER, "
+ KEY_NOTE + " INTEGER);");
Is it possible to set the default value of KEY_NOTE (which is an integer) for every row created to be 0 (zero)? If so, what should be the correct code?
Use the SQLite keyword default
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " ("
+ KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_NAME + " TEXT NOT NULL, "
+ KEY_WORKED + " INTEGER, "
+ KEY_NOTE + " INTEGER DEFAULT 0);");
This link is useful: http://www.sqlite.org/lang_createtable.html
A column with default value:
CREATE TABLE <TableName>(
...
<ColumnName> <Type> DEFAULT <DefaultValue>
...
)
<DefaultValue> is a placeholder for a:
value literal
( expression )
Examples:
Count INTEGER DEFAULT 0,
LastSeen TEXT DEFAULT (datetime('now'))
It happens that I'm just starting to learn coding and I needed something similar as you have just asked in SQLite (I´m using [SQLiteStudio] (3.1.1)).
It happens that you must define the column's 'Constraint' as 'Not Null' then entering your desired definition using 'Default' 'Constraint' or it will not work (I don't know if this is an SQLite or the program requirment).
Here is the code I used:
CREATE TABLE <MY_TABLE> (
<MY_TABLE_KEY> INTEGER UNIQUE
PRIMARY KEY,
<MY_TABLE_SERIAL> TEXT DEFAULT (<MY_VALUE>)
NOT NULL
<THE_REST_COLUMNS>
);
I have the following CLR function:
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlString PrintText(SqlString text)
{
// Put your code here
return new SqlString(text.Value);
}
And I want to get an enter symbol when passing '\r\n'
to it. But insteat I get '\r\n'
Could you please tell me what's wrong with this code.
Thank you.
In T-SQL you don't write a line break as \r\n. Instead you just use a line break:
'this
is a
string
in SQL
with
line breaks'
If you pass a string with \r\n to the C# code, nothing magical happens, it doesn't automatically get converted. The backslash character is just a character like any other. It's when you use the backslash in a literal string in the code that the compiler uses it as an escape code, and puts the control characters in the actual string.
You could always:
return new SqlString(text.Value.Replace("\\r\\n", "\r\n"));
From the SQL side, you could insert char(13) + char(10) into your T-SQL literals like so:
DECLARE #text varchar(100)
SET #test = 'this' + char(13) + char(10)
+ 'is a' + char(13) + char(10)
+ 'string' + char(13) + char(10)
+ 'in SQL' + char(13) + char(10)
+ 'with' + char(13) + char(10)
+ 'line breaks'
Though this works, it is much more verbose than Guffa's answer.
However, this technique can also be used to insert any character of the default code page into a string. The Function CHAR(int) accepts any integer between 0 and 255. Values outside that range cause it to return null. The function NCHAR(int) accepts values up to 65535 and inserts the corresponding unicode characters into a unicode string. Functions ASCII(char) and UNICODE(nchar) perform the inverse operations.