java.sql.SQLException:[SQLITE_ERROR] SQL错误或缺少数据库(附近"名称":语法错误)

时间:2016-04-21 11:56:47

标签: java sqlite javafx

我正在使用登录应用程序,用户应该可以登录。但是我收到此错误:

java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (near "Name": syntax error)

这就是我的代码的样子:

saveTButton.addEventHandler(ActionEvent.ACTION, (e)-> {

        try
        {
            String query = "insert into TeachersInfo (First Name,Last Name,UserName,Password) values (?,?,?,?)";
            PreparedStatement pst = connectionUsers.prepareStatement(query);
            //First question mark = 1, second = 2, etc.
            pst.setString(1,nameSignupTTextField.getText());
            pst.setString(2,lastnameSignupTTextField.getText());
            pst.setString(3,usernameSignupTTextField.getText());
            pst.setString(4,passwordSignupTTextField.getText());


            pst.execute();

            System.out.println("Data saved");

            //after executing the query this lines will close the connection with the database
            pst.close();

        }
        catch(Exception a)
        {
            System.out.println(a);
        }
    });

代码应保存用户在数据库中输入的信息。我不知道是什么问题所以我真的很感激帮助。提前谢谢..

4 个答案:

答案 0 :(得分:0)

由于您的列名称包含空格,您应该将它们转义:

 public class BackgroundTask extends AsyncTask<String,Void,String>
{

private Context context;
private Activity activity;
private String reg_url = "http://blaah.com/register.php";
private String login_url = "http://blaah.com/login.php";
private AlertDialog.Builder builder;
private ProgressDialog progressDialog;


public BackgroundTask(Context context)
{

    this.context = context;
    activity = (Activity) context;
}

@Override
protected void onPreExecute()
{
    builder = new AlertDialog.Builder(activity);
    progressDialog = new ProgressDialog(context);
    progressDialog.setTitle("Please wait");
    progressDialog.setMessage("Connecting to Server...");
    progressDialog.setIndeterminate(true);
    progressDialog.setCancelable(false);
    progressDialog.show();
}

@Override
protected String doInBackground(String... params)
{

    String method = params[0];

    if (method.equals("register"))
    {

        try
        {
            URL url = new URL(reg_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            String name = params[1];
            String email = params[2];
            String username = params[3];
            String password = params[4];
            String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" +
                    URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8") + "&" +
                    URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder stringBuilder = new StringBuilder();
            String line = "";
            while ((line = bufferedReader.readLine()) != null)
            {

                stringBuilder.append(line).append("\n");

            }


            httpURLConnection.disconnect();
            Thread.sleep(8000);
            return stringBuilder.toString().trim();

        } catch (MalformedURLException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }

    }
    else if (method.equals("login"))
    {
        try
        {
            URL url = new URL(login_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));


            String username,password;

            username = params[1];
            password = params [2];
            String data = URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" +
                    URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();

            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder stringBuilder = new StringBuilder();
            String line = "";
            while ((line = bufferedReader.readLine()) != null)
            {

                stringBuilder.append(line + "\n");

            }


            httpURLConnection.disconnect();
            Thread.sleep(5000);
            return stringBuilder.toString().trim();


        } catch (MalformedURLException e)
        {
            e.printStackTrace();
        } catch (ProtocolException e)
        {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }

    return null;
}

@Override
protected void onProgressUpdate(Void... values)
{
    super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(String json)
{
   try
   {

       progressDialog.dismiss();


       Log.v("JSON", json);
       JSONObject jsonObject = new JSONObject(json);
       JSONArray jsonArray = jsonObject.getJSONArray("server_response");
       JSONObject jsonobject = jsonArray.getJSONObject(0);
       String code = jsonobject.getString("code");
       String message = jsonobject.getString("message");



       if(code.equals("reg_true"))
       {
           showDialog("Sucessful registration.Thank you.Enjoy_AS!", message, code);
       }

       else if (code.equals("reg_false"))
       {
           showDialog("User Already exists", message, code);
       }

       else if (code.equals("login_true"))
       {
           Toast.makeText(context, "You are logged in", Toast.LENGTH_LONG).show();
           Intent intent = new Intent(activity, SplashScreen.class);
           activity.startActivity(intent);


       } else if (code.equals("login_false"))
       {
           showDialog("Login Error", message, code);
       }


   } catch (JSONException e){
       e.printStackTrace();
   }


}

public void  showDialog(String title,String message,String code)
{

    builder.setTitle(title);
    if (code.equals("reg_true") || code.equals("reg_false"))
    {
        builder.setMessage(message);//message form server
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener()

        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.dismiss();
                activity.finish();

            }
        });


    }

    else if (code.equals("login_false"))
    {


        builder.setMessage(message);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                EditText loginEmail, loginPassword;
                loginEmail = (EditText) activity.findViewById(R.id.email);
                loginPassword = (EditText) activity.findViewById(R.id.password);
                loginEmail.setText("");
                loginPassword.setText("");
                dialog.dismiss();

            }


        });


    }
    AlertDialog alertDialog = builder.create();
    alertDialog.show();
}

}

请参阅此主题:Read data from sqlite where column name contains spaces in android

答案 1 :(得分:0)

我不确定sql lite中的表列中是否允许空符号。但你可以试试这个:

insert into TeachersInfo ([First Name],[Last Name],UserName,Password) values (?,?,?,?)

否则尝试编辑列名称。

答案 2 :(得分:0)

我认为问题是列名First NameLast Name中的空格。如果那些真的是列名,我想你必须正确引用它们。但最好不要在列名中使用空格。

答案 3 :(得分:0)

不要在列名中使用空格。

       try
    {
        String query = "insert into TeachersInfo (First_Name,Last_Name,UserName,Password) values (?,?,?,?)";
        PreparedStatement pst = connectionUsers.prepareStatement(query);
        //First question mark = 1, second = 2, etc.
        pst.setString(1,nameSignupTTextField.getText());
        pst.setString(2,lastnameSignupTTextField.getText());
        pst.setString(3,usernameSignupTTextField.getText());
        pst.setString(4,passwordSignupTTextField.getText());


        pst.execute();

        System.out.println("Data saved");

        //after executing the query this lines will close the connection with the database
        pst.close();

    }
    catch(Exception a)
    {
        System.out.println(a);
    }
});
相关问题