Android Question Very long strings using multiple lines?

calloatti

Member
Licensed User
Is there any way to set a value to a string variable using multiple lines? For example instead of:

B4X:
Dim sql As String
sql = "CREATE TABLE IF NOT EXISTS stock('guid_stock' char(32) PRIMARY KEY NOT NULL, 'guid_arti' char(32) NOT NULL, 'idcaja' char(10) NOT NULL, 'serial' decimal(8,0) NOT NULL DEFAULT '0', 'serialprint' int(1) DEFAULT '0', 'tara' decimal(5,2) NOT NULL DEFAULT '0.00', 'bruto' decimal(5,2) NOT NULL DEFAULT '0.00', 'neto' decimal(5,2) NOT NULL DEFAULT '0.00', 'operador' char(2) NOT NULL DEFAULT '00', 'fecha' datetime, 'codigo' char(4) NOT NULL DEFAULT '0000', 'codigo1' char(2) NOT NULL DEFAULT '00', 'codigo2' char(1) NOT NULL DEFAULT '0', 'codigo3' char(1) NOT NULL DEFAULT '0', 'codigo4' char(1) NOT NULL DEFAULT '0', 'clave' char(2) NOT NULL DEFAULT '00', 'titulo' char(4) NOT NULL DEFAULT '00/0', 'tipo1' char(1) NOT NULL DEFAULT '0', 'tipo2' char(1) NOT NULL DEFAULT '0', 'tipo3' char(1) NOT NULL DEFAULT '0', 'nombre' varchar(45) NOT NULL DEFAULT '-', 'descrip' varchar(45) NOT NULL DEFAULT '-', 'lote' decimal(3,0) NOT NULL DEFAULT '0', 'turno' char(10) NOT NULL DEFAULT '0000000000', 'tfecha' char(8) NOT NULL DEFAULT '00000000', 'tturno' char(2) NOT NULL DEFAULT '00', 'estado' int(10), 'pedido' char(12) NOT NULL DEFAULT '00000000-000', 'romaneo' char(8) NOT NULL DEFAULT '00000000', 'origen' int(10) DEFAULT '0', 'imps' int(10) DEFAULT '0', 'etiquetas' int(10) DEFAULT '0')"

Something like this: (This can be done in Visual FoxPro, everything between text/endtext is treated as a string literal)

B4X:
Dim sql As String
text to sql
CREATE TABLE IF NOT EXISTS stock(
'guid_stock' char(32) PRIMARY KEY NOT NULL,
'guid_arti' char(32) NOT NULL,
'idcaja' char(10) NOT NULL,
'serial' decimal(8,0) NOT NULL DEFAULT '0',
'serialprint' int(1) DEFAULT '0',
'tara' decimal(5,2) NOT NULL DEFAULT '0.00',
'bruto' decimal(5,2) NOT NULL DEFAULT '0.00',
'neto' decimal(5,2) NOT NULL DEFAULT '0.00',
'operador' char(2) NOT NULL DEFAULT '00',
'fecha' datetime, 'codigo' char(4) NOT NULL DEFAULT '0000',
'codigo1' char(2) NOT NULL DEFAULT '00',
'codigo2' char(1) NOT NULL DEFAULT '0',
'codigo3' char(1) NOT NULL DEFAULT '0',
'codigo4' char(1) NOT NULL DEFAULT '0',
'clave' char(2) NOT NULL DEFAULT '00',
'titulo' char(4) NOT NULL DEFAULT '00/0',
'tipo1' char(1) NOT NULL DEFAULT '0',
'tipo2' char(1) NOT NULL DEFAULT '0',
'tipo3' char(1) NOT NULL DEFAULT '0',
'nombre' varchar(45) NOT NULL DEFAULT '-',
'descrip' varchar(45) NOT NULL DEFAULT '-',
'lote' decimal(3,0) NOT NULL DEFAULT '0',
'turno' char(10) NOT NULL DEFAULT '0000000000',
'tfecha' char(8) NOT NULL DEFAULT '00000000',
'tturno' char(2) NOT NULL DEFAULT '00',
'estado' int(10), 'pedido' char(12) NOT NULL DEFAULT '00000000-000',
'romaneo' char(8) NOT NULL DEFAULT '00000000',
'origen' int(10) DEFAULT '0',
'imps' int(10) DEFAULT '0',
'etiquetas' int(10) DEFAULT '0')
endtext
 

Mahares

Expert
Licensed User
Longtime User
Even when you use String Literals, you can still make your query smaller and more readable by assigning additional variables:. Example:
B4X:
Dim NND As String = "NOT NULL DEFAULT"  'makes code less verbose because you have many of them
strQuery= $"
CREATE TABLE If Not EXISTS stock(
guid_stock char(32) PRIMARY KEY NOT NULL,
guid_arti char(32) NOT NULL,
idcaja char(10) NOT NULL,
serial decimal(8,0) ${NND} '0',
serialprint int(1) DEFAULT '0',
tara decimal(5,2) ${NND} '0.00',
'below rest of your columns
Also, you do not need the confusing single quotes around the column names in your case, although they are harmless.
 
Upvote 0

calloatti

Member
Licensed User
Even when you use String Literals, you can still make your query smaller and more readable by assigning additional variables:. Example:
B4X:
Dim NND As String = "NOT NULL DEFAULT"  'makes code less verbose because you have many of them
strQuery= $"
CREATE TABLE If Not EXISTS stock(
guid_stock char(32) PRIMARY KEY NOT NULL,
guid_arti char(32) NOT NULL,
idcaja char(10) NOT NULL,
serial decimal(8,0) ${NND} '0',
serialprint int(1) DEFAULT '0',
tara decimal(5,2) ${NND} '0.00',
'below rest of your columns
Also, you do not need the confusing single quotes around the column names in your case, although they are harmless.
You are right, those single quotes should not be there. The create statement is actually extracted from the sqlite table schema, and the tables are originally created by a generic routine that creates sqlite tables from mysql tables. I guess at some point that routine included the single quotes. I deleted the table from sqlite, let it regenerate from the sqlite to mysql routine, and now the create statement does not have the single quotes.

The replacing "NOT NULL DEFAULT" would actually be more work in this case, since the create table statements are autogenerated as I mentioned before. Thanks.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
The replacing "NOT NULL DEFAULT" would actually be more work in this case
Since the focus of your thread is the string rather than the query, I wanted to illustrate how you can with string literal use a literal string inside another to make it more elegant and less repetitive. The reference to the query is of course a bonus.
 
Upvote 0
Top