简体   繁体   English

如何将表中的所有行获取到android应用

[英]How to get all rows from a table to an android app

my problem is i can't get all rows in a table to my android app my table is in a ms sql database and i want to get all the columns into a textview when i try to get them it only gets the first row in my table any help? 我的问题是我无法将表中的所有行都发送到我的android应用程序中,我的表在ms sql数据库中,并且当我尝试获取所有列时,我想将所有列都输入到textview中,因此它只能获取我的第一行表有什么帮助吗?

here is my code: 这是我的代码:

public class words extends AppCompatActivity {
private Button a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,rr,s,t,u,v,w,x,y,z,z1,z2;
    ConnectionClass connectionClass;
    ProgressBar loading;
    private TextView view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_words);
        getSupportActionBar().hide();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        a = (Button) findViewById(R.id.أ);
        b = (Button) findViewById(R.id.ب);
        c = (Button) findViewById(R.id.ت);
        d = (Button) findViewById(R.id.ث);
        e = (Button) findViewById(R.id.button3);
        f = (Button) findViewById(R.id.button13);
        g = (Button) findViewById(R.id.button17);
        h = (Button) findViewById(R.id.button6);
        i = (Button) findViewById(R.id.button8);
        j = (Button) findViewById(R.id.button18);
        k = (Button) findViewById(R.id.button19);
        l = (Button) findViewById(R.id.button21);
        m = (Button) findViewById(R.id.button26);
        n = (Button) findViewById(R.id.button22);
        o = (Button) findViewById(R.id.button23);
        p = (Button) findViewById(R.id.button24);
        q = (Button) findViewById(R.id.button20);
        rr = (Button) findViewById(R.id.button27);
        s = (Button) findViewById(R.id.button29);
        t = (Button) findViewById(R.id.button28);
        u = (Button) findViewById(R.id.button31);
        v = (Button) findViewById(R.id.button30);
        w = (Button) findViewById(R.id.button32);
        x = (Button) findViewById(R.id.button33);
        y = (Button) findViewById(R.id.button34);
        z = (Button) findViewById(R.id.button35);
        z1 = (Button) findViewById(R.id.button36);
        z2 = (Button) findViewById(R.id.button37);
        loading = (ProgressBar) findViewById(R.id.progressBar);
        connectionClass = new ConnectionClass();
        view = (TextView) findViewById(R.id.word);

        DoLogin doLogin = new DoLogin(); // this is the Asynctask
        doLogin.execute("");




    }
    public class DoLogin extends AsyncTask<String,String,String>
    {
        String message = "";
        Boolean isSuccess = false;








        @Override
        protected void onPreExecute() {
            loading.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onPostExecute(String r) {

            loading.setVisibility(View.GONE);
            Toast.makeText(words.this, r, Toast.LENGTH_SHORT).show();

            if(isSuccess) {
                Toast.makeText(words.this, r, Toast.LENGTH_SHORT).show();

            }

        }

        @Override
        protected String doInBackground(String... params) {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {

                    try {
                        Connection con = connectionClass.CONN();
                        if (con == null) {
                            message = "Error in connection with SQL server please contact the administrator";
                        } else {

                            String query = "SELECT * FROM country INNER JOIN family ON country.CID = family.CID";
                            Statement stmt = con.createStatement();
                            ResultSet rs = stmt.executeQuery(query);





                            if(rs.next())
                            {
                                try {
                                    String theword =  (rs.getString("FName"));

                                    if (theword.contains("أ")){

                                        a.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ب")){

                                        b.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ت")){

                                        c.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ث")){

                                        d.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ج")){

                                        e.setVisibility(View.VISIBLE);

                                    } else if (theword.contains("ح")){

                                        f.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("خ")){

                                        g.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("د")){

                                        h.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ذ")){

                                        i.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ر")){

                                        j.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ز")){

                                        k.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("س")){

                                        l.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ش")){

                                        m.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ص")){

                                        n.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ض")){

                                        o.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ط")){

                                        p.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ظ")){

                                        q.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ع")){

                                        rr.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("غ")){

                                        s.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ف")){

                                        t.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ق")){

                                        u.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ك")){

                                        v.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ل")){

                                        w.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("م")){

                                        x.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ن")){

                                        y.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ه")){

                                        z.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("و")){

                                        z1.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ي")){

                                        z2.setVisibility(View.VISIBLE);

                                    }else {
                                        message ="WERE IS THE FWORD COLUMN?!?!";
                                    }


                                    isSuccess=true;
                                }catch (Exception e){
                                    message = "Exception e = "+e;
                                    isSuccess = false;
                                }





                            }

                        }
                    }
                    catch (Exception ex)
                    {
                        isSuccess = false;
                        message = "Exceptions"+ex;

                    }


                }
            });

            return message;
            }
        }

    }

with this code it always give me in my text view only one row 使用此代码,它总是在我的文本视图中仅给我一行

any help?! 有帮助吗?

EDIT: 编辑:

THE QUERY RETURNS ALL ROWS SUCCESSFULLY BUT IT DOESN'T DO THE SAME THING IN THE APP 查询将成功返回所有行,但在应用程序中未执行相同的操作

SOLVED 解决了

I replaced if(rs.next()) with while(rs.next()) and it solved my problem! 我用while(rs.next())替换了if(rs.next()),它解决了我的问题!

First ensure your query is returning more than 1 row, execute it using any tool for that, 首先,请确保您的查询返回的行多于1行,为此使用任何工具执行查询,

SELECT column_name
FROM country 
INNER JOIN family 
ON country.CID = family.CID

If not, the issue comes with your INNER JOIN and change it to LEFT JOIN to return even the null values if that the case. 如果不是这样,则问题与您的INNER JOIN ,并将其更改为LEFT JOIN以在这种情况下甚至返回空值。

Select the column_name directly to avoid retrieve all columns you are not using. 直接选择column_name以避免检索未使用的所有列。

There can be two possibilities for your issue, 您的问题可能有两种可能性,

  1. Your selection query "SELECT * FROM country INNER JOIN family ON country.CID = family.CID" might be returning just a single row, verify that it returns all the required rows. 您的选择查询"SELECT * FROM country INNER JOIN family ON country.CID = family.CID"可能仅返回一行,请确认它返回了所有必需的行。 To verify that the total number of rows are available, you can attach a break-point at this line ResultSet rs = stmt.executeQuery(query); 要验证总行数可用,您可以在此行上附加一个断点ResultSet rs = stmt.executeQuery(query); and evaluate the left-hand side of the statement. 并评估该语句的左侧。

  2. Even if assumed, that your selection query is working fine and returning all the expected rows. 即使假设您的选择查询运行正常,并返回所有预期的行。 You need to iterate over the results( as suggested by @Psycho ) to fetch all rows from your query results. 您需要遍历结果(如@Psycho所建议),以从查询结果中获取所有行。

As the docs say: 正如文档所说:


A ResultSet object maintains a cursor pointing to its current row of data. ResultSet对象维护一个游标,该游标指向其当前数据行。 Initially, the cursor is positioned before the first row. 最初,光标位于第一行之前。 The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set. next方法将光标移动到下一行,并且由于当ResultSet对象中没有更多行时它返回false,因此可以在while循环中使用它来迭代结果集。 A default ResultSet object is not updatable and has a cursor that moves forward only. 默认的ResultSet对象是不可更新的,并且具有仅向前移动的光标。 Thus, you can iterate through it only once and only from the first row to the last row. 因此,您只能从第一行到最后一行迭代一次。


So what you can do is this, 所以你可以做的是

    if(rs.first()) //move the cusor to first position              
{
               do{
                                try {
                                    String theword =  (rs.getString("FName"));

                                    if (theword.contains("أ")){

                                        a.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ب")){

                                        b.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ت")){

                                        c.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ث")){

                                        d.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ج")){

                                        e.setVisibility(View.VISIBLE);

                                    } else if (theword.contains("ح")){

                                        f.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("خ")){

                                        g.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("د")){

                                        h.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ذ")){

                                        i.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ر")){

                                        j.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ز")){

                                        k.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("س")){

                                        l.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ش")){

                                        m.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ص")){

                                        n.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ض")){

                                        o.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ط")){

                                        p.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ظ")){

                                        q.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ع")){

                                        rr.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("غ")){

                                        s.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ف")){

                                        t.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ق")){

                                        u.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ك")){

                                        v.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ل")){

                                        w.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("م")){

                                        x.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ن")){

                                        y.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ه")){

                                        z.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("و")){

                                        z1.setVisibility(View.VISIBLE);

                                    }else if (theword.contains("ي")){

                                        z2.setVisibility(View.VISIBLE);

                                    }else {
                                        message ="WERE IS THE FWORD COLUMN?!?!";
                                    }


                                    isSuccess=true;
                                }catch (Exception e){
                                    message = "Exception e = "+e;
                                    isSuccess = false;
                                }

         }  while(rs.next()); //iterate till the next row is available  




                            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM