Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android上只插入一次sql查询_Android_Sql Server - Fatal编程技术网

如何在android上只插入一次sql查询

如何在android上只插入一次sql查询,android,sql-server,Android,Sql Server,我只需要一次插入sql查询的表 在插入查询之前。如果我的A表有1条记录。执行更新1记录 我的A表有0条记录。插入查询 public class compareInsert extends Thread { private Connection mConnection = null; private Statement mStatement = null; @Override public void run() { super.run();

我只需要一次插入sql查询的

在插入查询之前。如果我的
A
表有1条记录。执行更新1记录

我的
A
表有0条记录。插入查询

public class compareInsert extends Thread {

    private Connection mConnection = null;
    private Statement mStatement = null;

    @Override
    public void run() {
       super.run();

       Looper.prepare();
       setDB();
       Looper.loop();
    } 

    public void setDB() {
          mConnection = DBconnect.connectJDBC(); // DB Connect.

          try {
              mStatement = mConnection.createStatement();
              mStatement.executeQuery("use be");

              String insertq = "insert into A"
                       + "(name, old, num)"
                       + "values"
                       + "(?, ?, ?);
             PreparedStatement statement = mConnect.prepareStatement(insertq);
             statement.setString(1, "hyunwook");
             statement.setString(2, "17");
             statement.setString(3, "2732");

             int ret = statement.executeUpdate();
             if (ret > 0) {
                Log.d(TAG, "SUCCESS");
             } else {
                Log.d(TAG, "failed");
             }
          } catch (Exception e) {
               e.printStackTrace();
          } finally {
               try {
                    mConnection.close();
               } catch (Exception e) {
               }
          }
      }
   }
我只知道使用插入查询

public class compareInsert extends Thread {

    private Connection mConnection = null;
    private Statement mStatement = null;

    @Override
    public void run() {
       super.run();

       Looper.prepare();
       setDB();
       Looper.loop();
    } 

    public void setDB() {
          mConnection = DBconnect.connectJDBC(); // DB Connect.

          try {
              mStatement = mConnection.createStatement();
              mStatement.executeQuery("use be");

              String insertq = "insert into A"
                       + "(name, old, num)"
                       + "values"
                       + "(?, ?, ?);
             PreparedStatement statement = mConnect.prepareStatement(insertq);
             statement.setString(1, "hyunwook");
             statement.setString(2, "17");
             statement.setString(3, "2732");

             int ret = statement.executeUpdate();
             if (ret > 0) {
                Log.d(TAG, "SUCCESS");
             } else {
                Log.d(TAG, "failed");
             }
          } catch (Exception e) {
               e.printStackTrace();
          } finally {
               try {
                    mConnection.close();
               } catch (Exception e) {
               }
          }
      }
   }
此源代码是插入查询代码。 但应用程序启动时,始终插入查询

public class compareInsert extends Thread {

    private Connection mConnection = null;
    private Statement mStatement = null;

    @Override
    public void run() {
       super.run();

       Looper.prepare();
       setDB();
       Looper.loop();
    } 

    public void setDB() {
          mConnection = DBconnect.connectJDBC(); // DB Connect.

          try {
              mStatement = mConnection.createStatement();
              mStatement.executeQuery("use be");

              String insertq = "insert into A"
                       + "(name, old, num)"
                       + "values"
                       + "(?, ?, ?);
             PreparedStatement statement = mConnect.prepareStatement(insertq);
             statement.setString(1, "hyunwook");
             statement.setString(2, "17");
             statement.setString(3, "2732");

             int ret = statement.executeUpdate();
             if (ret > 0) {
                Log.d(TAG, "SUCCESS");
             } else {
                Log.d(TAG, "failed");
             }
          } catch (Exception e) {
               e.printStackTrace();
          } finally {
               try {
                    mConnection.close();
               } catch (Exception e) {
               }
          }
      }
   }
请帮我


谢谢。

使用插入或替换查询并在表中添加iD类型列,这将帮助您实现您的请求。使用插入或替换查询并在表中添加iD类型列,这将帮助您实现您的请求。