显示数据库问题的值

时间:2012-03-30 15:52:23

标签: android database listview datatable

package nidhin.survey;


import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class Database extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.database);

        ListView lv=(ListView)findViewById(R.id.mylist);
        lv.setClickable(true);

        Button button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {


        Intent intent=new Intent(Database.this,MainMenu.class);
            startActivity(intent);

            }
         });


        Intent sender=getIntent();

        DatabaseHelper dbh = new DatabaseHelper(this);

        Cursor c = dbh.getReadableDatabase().rawQuery("SELECT _id, " + 
                        DatabaseHelper.COL_NAME + 
                ", " + DatabaseHelper.COL_VALUE1 + 
                ", " + DatabaseHelper.VALUE2 + 
                ", " + DatabaseHelper.VALUE3 + 
                ", " + DatabaseHelper.VALUE4 + 
                ", " + DatabaseHelper.VALUE5 + 
                ", " + DatabaseHelper.VALUE6 + 
                ", " + DatabaseHelper.VALUE7 + 
                ", " + DatabaseHelper.VALUE8 + 
                ", " + DatabaseHelper.VALUE9 + 
                ", " + DatabaseHelper.VALUE10 + 
                ", " + DatabaseHelper.VALUE11 + 
                " FROM " +
                DatabaseHelper.TABLE_NAME, null); // initializing 

        String[] dataFrom = {DatabaseHelper.COL_NAME, DatabaseHelper.COL_VALUE1, DatabaseHelper.VALUE2, DatabaseHelper.VALUE3, DatabaseHelper.VALUE4, DatabaseHelper.VALUE5, DatabaseHelper.VALUE6, DatabaseHelper.VALUE7, DatabaseHelper.VALUE8, DatabaseHelper.VALUE9, DatabaseHelper.VALUE10, DatabaseHelper.VALUE11};
        int[] dataTo = {R.id.name, R.id.value1, R.id.value2, R.id.value3, R.id.value4, R.id.value5, R.id.value6, R.id.value7, R.id.value8, R.id.value9, R.id.value10, R.id.value11};

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
                R.layout.row, c, dataFrom, dataTo);

        lv.setAdapter(adapter);



    }

    public void onListItemClick()
    {

    }



}

DATABASEHELPER CLASS

package nidhin.survey;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import android.content.    import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {

static final String TABLE_NAME = "mygrades";
static final String COL_NAME = "name";
static final String COL_VALUE1 = "ClassNumber";
static final String VALUE2 = "value2";
static final String VALUE3 = "value3";
static final String VALUE4 = "value4";
static final String VALUE5 = "value5";
static final String VALUE6 = "value6";
static final String VALUE7 = "value7";
static final String VALUE8 = "value8";
static final String VALUE9 = "value9";
static final String VALUE10 = "value10";
static final String VALUE11 = "value11";


DatabaseHelper(Context context)
{
    super(context, "grades.sqlite", null, 4);
}

@Override
public void onCreate(SQLiteDatabase db) 
{


    db.execSQL("CREATE TABLE " + TABLE_NAME +
               "(_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
               COL_NAME + " TEXT, " + 
               COL_VALUE1 + " TEXT, " +
               VALUE2 + " TEXT, " +
               VALUE3 + " TEXT, " +
               VALUE4 + " TEXT, " +
               VALUE5 + " TEXT, " +
               VALUE6 + " TEXT, " +
               VALUE7 + " TEXT, " +
               VALUE8 + " TEXT, " +
               VALUE9 + " TEXT, " +
               VALUE10 + " TEXT, " +
               VALUE11 + " TEXT);");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{
    if (oldVersion >= newVersion)
        return;

    String sql = null;
    if (oldVersion == 1) 
        sql = "alter table " + TABLE_NAME + " add note text;";
    if (oldVersion == 2)
        sql = "";

    Log.d("EventsData", "onUpgrade  : " + sql);
    if (sql != null)
        db.execSQL(sql);
}

}

另一类增加价值的功能

public void addEvent ( String q1,String q2,String q3,String q4,String q5,String        q6,String q7,String q8,String q9,String q10,String q11)
{
SQLiteDatabase db = dbh2.getWritableDatabase();
 ContentValues values = new ContentValues();
    values.put(DatabaseHelper.COL_NAME, System.currentTimeMillis());
    values.put(DatabaseHelper.COL_VALUE1, q1);
    values.put(DatabaseHelper.VALUE2, q1);
    values.put(DatabaseHelper.VALUE3, q1);
    values.put(DatabaseHelper.VALUE4, q1);
    values.put(DatabaseHelper.VALUE5, q1);
    values.put(DatabaseHelper.VALUE6, q1);
    values.put(DatabaseHelper.VALUE7, q1);
    values.put(DatabaseHelper.VALUE8, q1);
    values.put(DatabaseHelper.VALUE9, q1);
    values.put(DatabaseHelper.VALUE10, q1);
    values.put(DatabaseHelper.VALUE11, q1);

    db.insert(DatabaseHelper.TABLE_NAME, null, values);

}


}

大家好,这似乎是一个简单的问题。我的数据库中有12列,我使用了SQLite数据浏览器,表格已成功构建。我有一个addEvent函数来向数据库添加值。唯一的问题是,当我回到主数据库类(显示表中的元素)时,我没有看到所有12列,只有2列。语法合适吗?任何想法?

0 个答案:

没有答案