0

我有以下代码在数据库中插入值:

import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class HNT {


    public static final String KEY_ROWID="_id";
    public static final String KEY_FName="person_Fname";
    public static final String KEY_LName="person_Lname";
    public static final String KEY_Gender="person_Gender";
    public static final String KEY_DOB="person_DOB"; 

    private static final String Database_Name="DBPersonInfo2";
    private static final String Database_Table="DBTable2";
    private static final int Database_Version=1;        


    private DBHelper  ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourDB;


    private static class DBHelper extends SQLiteOpenHelper{

        public DBHelper(Context context) {
            super(context, Database_Name, null, Database_Version);
            // TODO Auto-generated constructor stub
        }

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

            db.execSQL( "create table "+Database_Table+ "("+
                    KEY_ROWID+" integer primary key autoincrement,"  +           
                    KEY_FName+" TEXT NOT NULL, "+
                    KEY_LName+" TEXT NOT NULL, "+
                    KEY_Gender+" TEXT NOT NULL, "+
                    KEY_DOB+" Date NOT NULL);");

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int arg2) {
            // TODO Auto-generated method stub

            db.execSQL("drop table if exists"+ Database_Table );
            onCreate(db);
        }

    }

    public HNT (Context c)
    {
        ourContext=c;

    }

    public HNT open() throws SQLException
    {
        ourHelper=new DBHelper(ourContext);
        ourDB=ourHelper.getWritableDatabase();
        return this;    
    }

    public void close()
    {
        ourHelper.close();
    }

    public long createEntry(String fName, String lName, String gender, Date date) {
        // TODO Auto-generated method stub


        ContentValues cv=new ContentValues();
        cv.put(KEY_FName, fName);
        cv.put(KEY_LName, lName);
        cv.put(KEY_Gender, gender);
        cv.put(KEY_DOB, date);

        return ourDB.insert(Database_Table, null, cv);

    }

    public String getData() {
        // TODO Auto-generated method stub
        String[] columns=new String[]{KEY_ROWID,KEY_FName,KEY_LName};

        Cursor c=ourDB.query(Database_Table, columns, null, null, null, null, null);
        String result="";

        int iRow=c.getColumnIndex(KEY_ROWID);
        int iFName=c.getColumnIndex(KEY_FName);
        int iLName=c.getColumnIndex(KEY_LName);
        int iGender=c.getColumnIndex(KEY_Gender);
        int iDOB=c.getColumnIndex(KEY_DOB);

        for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
        {
            result=result+c.getString(iRow)+ " " + c.getString(iFName)+ " " + c.getString(iLName)+  c.getString(iGender)+ " " +  c.getString(iDOB)+ " "+"\n";
        }
        return result;
    }

}

此代码在行显示错误:cv.put(KEY_DOB, date);说明method put(string,string) is not applicable for arguments (String,Date)

请帮我在数据库中插入日期数据类型。

4

1 回答 1

3

将您的日期转换为字符串格式,然后插入数据库

String date = new SimpleDateFormat("yyyy/MM/dd").format(new Date()).toString().trim();

然后它会工作

于 2013-08-19T06:28:26.833 回答