Home > Software design >  Java Sqlite how to print a.rowname
Java Sqlite how to print a.rowname

Time:12-19

I asked myself how I print a.rowname in Java with jbdc sqlite. Or did I take the wrong solution to fetch all the Selected data?

I tried this:

String vergleichVon ="DB_18_12_2021_02h13m05s";
String vergleichBis ="DB_18_12_2021_08h28m19s";

Connection connection = null;
try
{
    //Connect to Database
    connection = DriverManager.getConnection("jdbc:sqlite:" dataBasePath "");
    Statement c = connection.createStatement();
    c.setQueryTimeout(30);  // set timeout to 30 sec.

    ResultSet rs = c.executeQuery("SELECT a.name, a.pt, b.pt, a.sz, a.bd" 
                                    "FROM " vergleichVon " AS a, " vergleichBis " AS b " 
                                    "WHERE a.name= b.name AND a.pt>'1000' AND b.pt>='1000' AND a.pt!='n/v' AND b.pt!='n/v' " 
                                    "ORDER BY a.pt DESC");
    while(rs.next())
    {
      // read the result set
      System.out.print("nameA = "   rs.getString("a.name"));
      System.out.print(" ");
      System.out.println("ptB = "   rs.getString("b.pt"));
    }
}

And I get this error:

no such column: 'a.name' 

In Python it works kind of like this. I can later just get the Selected data from the array:

c.execute('''SELECT a.name, a.pt, b.pt, a.sz, a.bd
          FROM ''' vergleich_von ''' AS a, ''' vergleich_bis ''' AS b
          WHERE a.name= b.name AND a.pt>'1000' AND b.pt>='1000' AND a.pt!='n/v' AND b.pt!='n/v'
          ORDER BY a.pt DESC''')
array = c.fetchall()

CodePudding user response:

You do not select a.spieler

try:

ResultSet rs = c.executeQuery("SELECT a.name as spieler, a.pt, b.pt as bpt, a.sz, a.bd" 
                                "FROM " vergleichVon " AS a, " vergleichBis " AS b " 
                                "WHERE a.name= b.name AND a.pt>'1000' AND b.pt>='1000' AND a.pt!='n/v' AND b.pt!='n/v' " 
                                "ORDER BY a.pt DESC");
while(rs.next())
{
  // read the result set
  System.out.print("nameA = "   rs.getString("spieler"));
  System.out.print(" ");
  System.out.println("ptB = "   rs.getString("bpt"));
}

Update after question edit Use aliases for the columns without dots:

ResultSet rs = c.executeQuery("SELECT a.name as name, a.pt as apt, b.pt as bpt, a.sz as asz, a.bd as adb" 
                                "FROM " vergleichVon " AS a, " vergleichBis " AS b " 
                                "WHERE a.name= b.name AND a.pt>'1000' AND b.pt>='1000' AND a.pt!='n/v' AND b.pt!='n/v' " 
                                "ORDER BY a.pt DESC");
while(rs.next())
{
  // read the result set
  System.out.print("nameA = "   rs.getString("name"));
  System.out.print(" ");
  System.out.println("ptB = "   rs.getString("bpt"));
}
  • Related