Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

SQLite query cannot inner join tables in Java, but the query works on SQLite Man

ID: 3727604 • Letter: S

Question

SQLite query cannot inner join tables in Java, but the query works on SQLite Manager.

Query results on SQLite Manager:

Select t1.Table1Test1 AS T1T1, t2.Table2Test1 AS T2T1, t3.Table3Test1 AS T3T1, t4.Table4Test1 AS T4T1
From Table1 t1 INNER JOIN Table2 t2 ON t1.Table1PK = t2.Table1PK
INNER JOIN Table3 t3 ON t2.Table2PK = t3.Table2PK
INNER JOIN Table4 t4 ON t3.Table3PK = t4.Table3PK
WHERE T1T1 = "table1test1"

Query Results on Java, it gives an error when inner joining tables in Java (The query is the same on SQLite and Java, but different results):

When I don't inner join tables in Java, the query works:

final String sql = "Select t1.Table1Test1 AS T1T1,t1.Table1Test1 AS T2T1, t1.Table1Test1 AS T3T1,t1.Table1Test1 AS T4T1"
+ " FROM Table1 t1 WHERE t1.Table1Test1 = ?";

Here is a link to Google Drive for the database, Create Table, Insert Value and Query files: https://drive.google.com/open?id=1vrWDtAh8qIkVECWT030IHNXJkN0Ctq9Y

Please look at the table I created and why query with inner join works on SQLite, but doesnt work when executing it in Java.

1 Select t1.Table1Testl AS T1T1, t2.Table2Testl AS T2T1, t3.Table3Test1 AS T3T1, t4.Table4Testl AS T4T1 2 From Table1 t1 INNER JOIN Table2 t2 ON t1·Table 1 PK = t2·Table 1 PK INNER JOIN Table3 t3 ON t2.Table2PK-t3,Table2 PK 4 INNER JOIN Table4 t4 ON t3.Table3PK t4.Table3PK wHERE T1T1 = 'table 1 test!' T1T1 T2T1 T3T1 T4T1 1 table1test1 table2test1 table3test1 table4test1

Explanation / Answer

Undoubtedly there is no error on your sql query, it will run nd gives your the result back, but what I expect is the error in your final string declaration part which contains the sql string. Here I see as there is no space between the string conctenation.

While doing such type of string conctention you must be careful on this mtter,

Let check once again the final declration of your string, you must specify one white space after/before you concat the string using + operator when the string starts.

so let me correct your declaration.

the corrected string is:

final String sql= "Select t1.Table1Test1 AS T1T1, t2.Table2Test1 AS T2T1, t3.Table3Test1 AS T3T1, t4.Table4Test1 AS T4T1 "+
"From Table1 t1 INNER JOIN Table2 t2 ON t1.Table1PK = t2.Table1PK "+
"INNER JOIN Table3 t3 ON t2.Table2PK = t3.Table2PK "+
"INNER JOIN Table4 t4 ON t3.Table3PK = t4.Table3PK "+
"WHERE t1.Table1Test1 = ?";

Please use the above given code, it will give the correct output