显示来自sqlDatabase的多个表

时间:2018-10-24 14:33:42

标签: android database sqlite

我正在开发应用程序报价,并且正在使用本地数据库导入报价。
这是sqldatabase中制作的4个表。

enter image description here

在这里结合 Table Database

我的表名是“ be.db” 我把它放在android studio的资产文件夹中

如您在图片中所见,标题“生命”具有多个引号 因为在应用程序中,当我想单击项目“生活”时,我希望它向我显示第一个报价,然后我将其滑到另一个报价,这里是应用程序的前面

front page of app

我不知道如何从sqldatabase导入多个或3个表的问题,我只知道如何导入一个表,正如您在下面看到的我在android studio中“ DatabaseHelper”类中的代码一样:

public class DataBaseHelper extends SQLiteOpenHelper {



public static final String DBNAME="be.db";
public static final String DBLOCATION=Environment.getDataDirectory()+"/data/com.example.z210.story/databases/";

private Context mcontext;
private SQLiteDatabase mDatabase;


public DataBaseHelper(Context context) {
    super(context,DBNAME,null,1);
    this.mcontext=context;
}

@Override
public void onCreate(SQLiteDatabase db) {
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}


public void openDatabase() {
    //l'acces au notre base de donnees
    String dbPath=mcontext.getDatabasePath(DBNAME).getPath();
    if(mDatabase!=null && mDatabase.isOpen()){
        return;
    }
    mDatabase=SQLiteDatabase.openDatabase(dbPath,null,SQLiteDatabase.OPEN_READWRITE);
}

public void closeDatabbase(){

    if(mDatabase !=null){
        mDatabase.close();
    }
}



public ArrayList getAllTitles(){
    ArrayList arrayList=new ArrayList();
    openDatabase();
    Cursor res=mDatabase.rawQuery("select * from title ",null);
    res.moveToFirst();
    while(!res.isAfterLast()){
        arrayList.add(res.getString(res.getColumnIndex("title")));
        res.moveToNext();
    }
    res.close();
    closeDatabbase();
    return arrayList;
}
public ArrayList getAllQuotes(){
    ArrayList arrayList=new ArrayList();
    openDatabase();
    Cursor res=mDatabase.rawQuery("select * from quote ",null);
    res.moveToFirst();
    while(!res.isAfterLast()){
        arrayList.add(res.getString(res.getColumnIndex("quote")));
        res.moveToNext();
    }
    res.close();
    closeDatabbase();
    return arrayList;
}

public String get_full_story(String title){
    String full_story;
    openDatabase();
    Cursor res =mDatabase.rawQuery("select * from title where quote like '"+getAllQuotes() +"'",null);
    res.moveToFirst();
    full_story=res.getString(res.getColumnIndex("quote"));
    res.close();
    closeDatabbase();
    return full_story;

}

}

这是我在主要活动中的代码

public class MainActivity extends AppCompatActivity   {

DataBaseHelper db =new DataBaseHelper (this);

private DrawerLayout mDrawerLayout;
TextView textViewSub,textView3;
Typeface tf1,tf2;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     ListView listView=(ListView)findViewById(R.id.list);



    File database=getApplicationContext().getDatabasePath(db.DBNAME);
    if(false==database.exists()) {
        db.getDatabaseName();
        if (copyDatabase(this)) {

        } else {

            return;
        }
    }



    ArrayList listTitles=db.getAllTitles();
    ArrayList listTitle=db.getAllQuotes();

    ArrayAdapter arrayAdapter=new ArrayAdapter(this,R.layout.row_itm,R.id.textView3,listTitles);
    listView.setAdapter(arrayAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            String az=String.valueOf(parent.getItemAtPosition(position));

            Intent intent=new Intent(MainActivity.this,ShowActivity.class);

            intent.putExtra("p",az);
            startActivity(intent);
        }
    });







    mDrawerLayout = findViewById(R.id.drawer);
    NavigationView navigationView = findViewById(R.id.nav_view);

    navigationView.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {


                    menuItem.setChecked(true);


                    mDrawerLayout.closeDrawers();



                    return true;
                }
            });

    mDrawerLayout.addDrawerListener(
            new DrawerLayout.DrawerListener() {
                @Override
                public void onDrawerSlide(View drawerView, float slideOffset)     {

                }

                @Override
                public void onDrawerOpened(View drawerView) {

                }

                @Override
                public void onDrawerClosed(View drawerView) {

                }

                @Override
                public void onDrawerStateChanged(int newState) {

                }
            }
    );

    textViewSub=(TextView)findViewById(R.id.textViewSub);
    textView3=(TextView)findViewById(R.id.textView3);


    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ActionBar actionbar = getSupportActionBar();
    actionbar.setDisplayHomeAsUpEnabled(true);
    actionbar.setHomeAsUpIndicator(R.drawable.para);



    final String[] itm=getResources().getStringArray(R.array.index);

 (this,R.layout.row_itm,R.id.textView3,itm);




    tf1=Typeface.createFromAsset(getAssets(),"orangeblossoms.ttf");
    textViewSub.setTypeface(tf1);

}




private boolean copyDatabase(Context context) {
    try {
        InputStream inputStream=context.getAssets().open(db.DBNAME);
        String outFileName=db.DBLOCATION+db.DBNAME;
        OutputStream outputStream=new FileOutputStream(outFileName);
        byte[] buff=new byte[1024];
        int lenght=0;
        while ((lenght=inputStream.read(buff))>0){
            outputStream.write(buff,0,lenght);
        }
        outputStream.flush();
        outputStream.close();
        return true;

    } catch (Exception e) {
        return  false;
    }

}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            mDrawerLayout.openDrawer(GravityCompat.START);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

public void error(View view) {
finish();
}


public void favo(View view) {
}
}

1 个答案:

答案 0 :(得分:0)

先生,

它应该使用String数组或类/对象来检索结果,而不仅仅是字符串。

您的SQL调用返回结果集游标,而不是String。概念上是错误的。