将值添加到SQLite数据库

时间:2014-06-05 00:57:43

标签: java android sqlite

我有一个问题,我一直试图解决很长一段时间。我通过了许多问题,包括强制关闭和感谢上帝我成功地解决了它。然而,现在,我已经达到了一个步骤,在logcat中没有显示任何问题,但是我没有达到预期的结果。

这是我的数据库:

      package SQL;


       import java.util.LinkedList;
       import java.util.List;
       import android.content.ContentValues;
       import android.content.Context;
       import android.database.Cursor;
       import android.database.sqlite.SQLiteDatabase;
       import android.database.sqlite.SQLiteOpenHelper;

       public class Database extends SQLiteOpenHelper{






public static final String id_sub = "id_sub";

public final String table_sub= "subjects";

public static final String table_hw = "homeworks";
public static final String table_marks = "marks";
public static final String table_q = "quizzes";


        public static final String id_mark = "id_m";
        public static final String id_hw= "id_hw";
        public static final String id_q = "id_q";
        public static final String SSubject ="subject";
        public static final String Mark= "mark";
        public static final String Homework = "homework";
        public static final String Quiz = "quiz";
        public static final String name = "iSchool";
        public static final int version = 1;



       public Database(Context context) {
    super(context, name, null, version);
    // TODO Auto-generated constructor stub

}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub

    db.execSQL("CREATE TABLE "+ table_sub +" ( "+id_sub +" INTEGER PRIMARY KEY 
            AUTOINCREMENT , "+ SSubject +" TEXT NOT NULL );");


    db.execSQL("CREATE TABLE "+ table_marks+" ( "+ id_mark +" INTEGER PRIMARY KEY 
         AUTOINCREMENT , "+ Mark
            +" TEXT NOT NULL, "+SSubject+" TEXT NOT NULL);" );

    db.execSQL("CREATE TABLE "+ table_hw +" ( "+ id_hw +" INTEGER PRIMARY KEY 
        AUTOINCREMENT , "+SSubject
            +" TEXT NOT NULL, "+Homework+" TEXT NOT NULL ) ;");

    db.execSQL("CREATE TABLE "+ table_q +" ( "+ id_q +" INTEGER PRIMARY KEY 
       AUTOINCREMENT, " +SSubject
            +" TEXT NOT NULL, "+Quiz+" TEXT NOT NULL ) ;");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS "+table_sub );
    db.execSQL("DROP TABLE IF EXISTS " +table_hw);
    db.execSQL("DROP TABLE IF EXISTS "+ table_q);
    db.execSQL("DROP TABLE IF EXISTS "+ table_marks);

    onCreate(db);
        }
         //-------------------------------------------Todo Table 
             CRUD------------------------------------\\
        public Subject getSubject(long subs_id){
            SQLiteDatabase db  = this.getReadableDatabase();


            Cursor c = db.query(table_sub, new String[]{id_sub,SSubject}," id 
           =? ",new String[]{String.valueOf(id_sub)},null,null,null,null);

            if(c != null){
                c.moveToFirst();
            }
            Subject sub = new Subject();
            sub.setId(Integer.parseInt(c.getString(0)));
            sub.setSubjectstr(c.getString(1));
            return sub;

        }

        public   void addSubject(Subject subject){
                SQLiteDatabase db = this.getWritableDatabase();
            String sq = "SELECT*FROM "+ table_sub;
            Cursor c = db.rawQuery(sq,null);
            ContentValues contentValues = new ContentValues();
            contentValues.put(SSubject, subject.getSubjectstr());

             db.insert(table_sub, null , contentValues);
            db.close();
        }


        public List<Subject> getSubjects(){
            SQLiteDatabase db = this.getReadableDatabase();
            String sss = "SELECT*FROM "+ table_sub;
            Cursor c = db.rawQuery(sss, null);
            List<Subject> list = new LinkedList<Subject>();

            if(c.moveToFirst()){

                do{Subject sub = new Subject();
                sub.setId(Integer.parseInt(c.getString(0)));
                sub.setSubjectstr(c.getString(1));
                    list.add(sub);
                }while(c.moveToNext());
            }

            return list;                
        }

        public int updateSub(Subject sub){
        SQLiteDatabase db = this.getWritableDatabase();

            ContentValues cv = new ContentValues();
            cv.put("id_sub", sub.getId());
            cv.put("SSubject", sub.getSubjectstr());


                int i = db.update(table_sub,cv , id_sub+ " =? ",new String[]
    {String.valueOf(sub.getId())});
                    return i;
        }


        public void deleteSub(Subject sub){
            SQLiteDatabase db = this.getWritableDatabase();

            db.delete(table_sub, id_sub+ " =? ",new String[]{String.valueOf
     (sub.getId())});
            db.close();
        }



    }

这是我使用数据库的Activity

     package com.Turki.ischool;


         import java.util.ArrayList;
         import java.util.List;
         import SQL.Database;
         import SQL.Subject;
         import android.app.Activity;
         import android.app.AlertDialog;
         import android.app.ListActivity;
         import android.content.DialogInterface;
         import android.database.Cursor;
         import android.database.sqlite.SQLiteDatabase;
         import android.os.Bundle;
         import android.view.LayoutInflater;
         import android.view.Menu;
         import android.view.MenuItem;
         import android.view.View;
         import android.widget.ArrayAdapter;
         import android.widget.EditText;
         import android.widget.ListView;

     public class FirstRun extends Activity {
    String subido;
Database helper;
Cursor c;
EditText et;
ListView lv;
String subs;
List<Subject> ss;
SQLiteDatabase db;
String[] fromColumns = {helper.SSubject};
int[] toViews = {R.id.subber};
AlertDialog.Builder ad;
AlertDialog alert;
EditText addBar;



public void ADialog(){
    LayoutInflater inflater = LayoutInflater.from(this);
    View prompt = inflater.inflate(R.layout.dialog,null);
    AlertDialog.Builder adb = new AlertDialog.Builder(this);
    adb.setView(prompt);

     addBar = (EditText)prompt.findViewById(R.id.addBar);

    adb
    .setCancelable(true)
    .setNeutralButton("Next", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

        }
    })
    .setPositiveButton("Add",new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
        subido = addBar.getText().toString();
        adder();

        }
    })
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            dialog.cancel();
        }
    })
    .setTitle("Add Subject");


     alert = adb.create();
     alert.show();
}


public void adder() {
    //Initialization

    helper = new Database(this);
    db = helper.getWritableDatabase();
    //Adding a Subject
    helper.addSubject(new Subject(subido));

    db.close();
    helper.close();
    }



@Override
protected void onCreate(Bundle savedInstanceState) {

    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.first);

    //Initialization

            helper = new Database(this);
            lv = (ListView)findViewById(R.id.listView1);
            List<Subject> list = helper.getSubjects();
            ArrayList<Subject> list2 = new ArrayList<Subject>();

            for(Subject list3 : list){
                list2.add(list3);
            }
            ArrayAdapter<Subject> aa = new ArrayAdapter<Subject>(this, 
     android.R.layout.simple_list_item_1, list2);
            lv.setAdapter(aa);


}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub

    getMenuInflater().inflate(R.menu.first_run, menu);

    return super.onCreateOptionsMenu(menu);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub

    switch(item.getItemId()){
    case R.id.actionAdd :
        ADialog();


    }
    return super.onOptionsItemSelected(item);
}

         }




        knowing that the logcat doesn't display any problem

提醒:问题是listView中没有显示任何内容,或者没有正确地向数据库添加项目。

更新:我的应用程序似乎实际上在列表视图中显示了一个结果。但它会在很多时间后显示它,并显示如下内容:SQL.Subject@42103b88

2 个答案:

答案 0 :(得分:0)

matiash?如果你逃避并解析你的东西,rawQuery很好。如果您计划使用连接或复杂的查询,则需要使用rawQuery。

答案 1 :(得分:0)

谢谢大家但是我通过在oncrreate方法和adder方法上放置了read方法来解决我的问题。我也将getSubjects()的类型更改为List,并将列表设置为取得sub.getSubjectstr的值()并且工作正常。