在android中的数据库中插入数据

时间:2014-03-22 08:18:54

标签: android sqlite

我在android中工作,并尝试在数据库中插入值,这是我的数据库类的代码

public class DataHandler {
    public static final String fname="fname";
    public static final String lname="lname";
    public static final String age="21";
    public static final String height="180";
    public static final String weight="50";
    public static final String TABLE_NAME="user";

    public static final int  DATABASE_VERSION=1;
    public static final String DATA_BASE_NAME="SLIMART_DATABASE";
    public static final String TABLE_CREATE ="create table user(fname text not null, lname text not null, age text not null, height text not null,weight text not null);";

    DataBaseHelper dbhelper;
    Context ctx;
    SQLiteDatabase db;
    public DataHandler(Context ctx)
    {
        this.ctx=ctx;
        dbhelper=new DataBaseHelper(ctx);
    }
    private static class DataBaseHelper extends SQLiteOpenHelper
    {
        public DataBaseHelper(Context ctx)
        {
            super(ctx,DATA_BASE_NAME,null,DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            try{
            db.execSQL(TABLE_CREATE);
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS user");
            onCreate(db);

        }

    }
    public DataHandler open()
    {
        db=dbhelper.getWritableDatabase();
        return this;
    }
    public void close()
    {
        dbhelper.close();
    }

    public long insertData(String afname,String alname,String aage,String aheight,String aweight)
    {
        ContentValues content =new ContentValues();
        content.put(fname,afname);
        content.put(lname,alname);
        content.put(age,aage);
        content.put(height,aheight);
        content.put(weight,aweight);
        return db.insertOrThrow(TABLE_NAME, null, content);

    }

    public Cursor returnData()
    {
        return db.query(TABLE_NAME,new String[] {fname,lname,age,height,weight},null,null,null,null,null);
    }



}

这是我访问数据库类的活动代码

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signup);

        save=(Button) findViewById(R.id.button1);
        fname=(EditText) findViewById(R.id.fname);
        lname=(EditText) findViewById(R.id.lname);
        age=(EditText) findViewById(R.id.age);
        height=(EditText) findViewById(R.id.height);
        weight=(EditText) findViewById(R.id.weight);

        save.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) 
            {
                String sfname=fname.getText().toString();
                String slname=lname.getText().toString();
                String sage=age.getText().toString();
                String sheight=height.getText().toString();
                String sweight=weight.getText().toString();

                handler = new DataHandler(getBaseContext());
                handler.open();
                long id=handler.insertData(sfname, slname, sage, sheight, sweight);
                Toast.makeText(getBaseContext(),"Data Successfully Saved",Toast.LENGTH_LONG).show();
                handler.close();

            }


        });

    }

但是我在logcat中收到了这个错误

03-22 03:16:37.060: E/AndroidRuntime(1115): android.database.sqlite.SQLiteException: near "0": syntax error (code 1): , while compiling: INSERT INTO user(lname,0,fname) VALUES (?,?,?)

2 个答案:

答案 0 :(得分:1)

content.put(age,aage);
content.put(height,aheight);
content.put(weight,aweight);

public static final String age="21";
public static final String height="180";
public static final String weight="50";

ageheightweight不是表格中的列名,而只是数字文字。将实际列名称用作content键。此外,您不能将列名称只是数字。

例如,更改为:

content.put("age",aage);
content.put("height",aheight);
content.put("weight",aweight);

答案 1 :(得分:0)

我检查了你的代码&我看到了问题,这是您插入数据的代码。

public long insertData(String afname,String alname,String aage,String aheight,String aweight)
    {
        ContentValues content =new ContentValues();
        content.put(fname,afname);
        content.put(lname,alname);
        content.put(age,aage);
        content.put(height,aheight);
        content.put(weight,aweight);
        return db.insertOrThrow(TABLE_NAME, null, content);

    }

- >其中:fname,lname,age,weight,height是数据库表中字段的名称。但你也定义:

public static final String fname="fname";
    public static final String lname="lname";
    public static final String age="21";
    public static final String height="180";
    public static final String weight="50";

- >创建数据库表时,年龄,身高,体重不是字段的名称。
所以:请改变如下:

public static final String fname="fname";
    public static final String lname="lname";
    public static final String age="age";
    public static final String height="height";
    public static final String weight="weight";