我在程序中使用SQlite和android studio

时间:2017-02-18 08:18:42

标签: android sqlite

public class Project {
int _id ;
String _name;
String _pprice ;
String _start ;
String _manhours ;
String _finish ;

public Project(int id, String name, String pprice, String start, String manhours, String finish) {
    this._id=id;
    this._name=name;
    this._pprice=pprice;
    this._start=start;
    this._manhours=manhours;
    this._finish=finish;
}
public Project( String name, String pprice, String start, String manhours, String finish) {
    this._name=name;
    this._pprice=pprice;
    this._start=start;
    this._manhours=manhours;
    this._finish=finish;
}
public int getprojid(){
    return this._id;
}
public String getprojName(){
    return this._name;
}
public String getpvalue(){
    return this._pprice;
}
public String getstartdate(){ return this._start; }
public String getmanhours(){
    return this._manhours;
}
public String getfinishdate(){ return this._finish; }

public void setprojid(int projid){ this._id = projid;}
public void setprojname(String projname){ this._name = projname;}
public void setprojvalue(String ppprice){ this._pprice = ppprice;}
public void setstartdate(String startdate){this._start = startdate;}
//   public void setduration(String duration){this._DURATION = duration;}
public void setprojmanhours(String pmanhours){
    this._manhours = pmanhours;
}
public void setfinishdate(String finishdate){
    this._finish = finishdate;
}
}

SQLiteHelper类

public class popopHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "projects4_Manager";

// Contacts table name
private static final String TABLE_CONTACTS = "proj4";

// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_PRICE = "price";
private static final String KEY_START = "start";
private static final String KEY_MANHOURS="pmanhours";
private static final String KEY_FINISH = "finish";

public popopHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION );
}


@Override


public void onCreate(SQLiteDatabase sqLiteDatabase) {


    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + "TEXT,"+ KEY_PRICE + "INTEGER,"
            + KEY_START + " TEXT," + KEY_MANHOURS + "INTEGER,"
            + KEY_FINISH + " TEXT" + ")";
    sqLiteDatabase.execSQL(CREATE_CONTACTS_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

    // Create tables again
    onCreate(sqLiteDatabase);

}
// Adding new contact

public void addproject(Project project) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, project.getprojName()); // Contact Name
    values.put(KEY_PRICE, project.getpvalue());
    values.put(KEY_START,project.getstartdate());
    values.put(KEY_MANHOURS, project.getmanhours());
    values.put(KEY_FINISH, project.getfinishdate());

    // Inserting Row
    db.insert(TABLE_CONTACTS, null, values);
    db.close(); // Closing database connection
}

public Project getProject(String proname) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_CONTACTS, new String[]{KEY_ID,
                    KEY_NAME,KEY_PRICE,KEY_START,KEY_MANHOURS,KEY_FINISH},KEY_NAME + "=?",
            new String[]{String.valueOf(proname)}, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    Project project = new Project(Integer.parseInt(cursor.getString(cursor.getColumnIndex(KEY_ID))),
            cursor.getString(cursor.getColumnIndex(KEY_NAME)),
            cursor.getString(cursor.getColumnIndex(KEY_PRICE)),
            cursor.getString(cursor.getColumnIndex(KEY_START)),
            cursor.getString(cursor.getColumnIndex(KEY_MANHOURS)),
            cursor.getString(cursor.getColumnIndex(KEY_FINISH)));
    return project;
}

public String updateproject(Project project) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, project.getprojName()); // Contact Name
    values.put(KEY_PRICE, project.getpvalue());
    values.put(KEY_START,project.getfinishdate());
    values.put(KEY_MANHOURS,project.getmanhours());
    values.put(KEY_FINISH, project.getstartdate());

    // updating row

    return String.valueOf(db.update(TABLE_CONTACTS, values,KEY_NAME+ " = ?",
            new String[]{String.valueOf(project.getprojName())}));
}

// Deleting single contact

public void deleteproject(Project project) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_CONTACTS,KEY_NAME + " = ?",
            new String[]{String.valueOf(project.getprojName())});
    db.close();
}
}

活动类

  public class AdProject2 extends AppCompatActivity {

private static final String TAG = "AdProject2";

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final popopHandler db = new popopHandler(AdProject2.this);
    setContentView(R.layout.addproject2);
    final EditText value;
    final EditText name;
    final EditText start;
    final EditText manhours;
    final EditText finish;
    value = (EditText) findViewById(R.id.projectvalue);
    name = (EditText) findViewById(R.id.myprojectname);
    manhours = (EditText) findViewById(R.id.projectmanhour);
    start = (EditText) findViewById(R.id.startdate);
    finish = (EditText) findViewById(R.id.finishdate);
 //   value.addTextChangedListener(onTextChangedListener());
    // final TextInputLayout projnamelayout = (TextInputLayout) findViewById(firstnameInputLayout);
    Button saveButton = (Button) findViewById(R.id.saveButton);
    saveButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            db.addproject(new Project(name.getText().toString(),
                    value.getText().toString(),
                    start.getText().toString(), manhours.getText().toString(),
                    finish.getText().toString()));

        }
    });
}
}

我的错误讯息:

  

E / SQLiteLog:(1)表proj4没有名为pmanhours的列   E / SQLiteDatabase:插入完成时出错= 11/6/2018 pmanhours = 55555   price = 885 start = 11/6/2016 name = ff                     android.database.sqlite.SQLiteException:table proj4没有名为pmanhours的列(代码1):,同时编译:INSERT INTO   proj4(完成,pmanhours,价格,开始,名称)VALUES(?,?,?,?,?)                         在android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native   方法)                         在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1120)                         在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:693)                         在android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)                         在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58)                         在android.database.sqlite.SQLiteStatement。(SQLiteStatement.java:31)                         在android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1589)                         在android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1461)                         在pioneers.safwat.earnedvaluecalculations.popopHandler.addproject(popopHandler.java:73)                         at pioneers.safwat.earnedvaluecalculations.AdProject2 $ 1.onClick(AdProject2.java:39)                         在android.view.View.performClick(View.java:4476)                         在android.view.View $ PerformClick.run(View.java:18795)                         在android.os.Handler.handleCallback(Handler.java:730)                         在android.os.Handler.dispatchMessage(Handler.java:92)                         在android.os.Looper.loop(Looper.java:177)                         在android.app.ActivityThread.main(ActivityThread.java:5496)                         at java.lang.reflect.Method.invokeNative(Native Method)                         在java.lang.reflect.Method.invoke(Method.java:525)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1225)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)                         at dalvik.system.NativeStart.main(Native Method)W / IInputConnectionWrapper:getTextBeforeCursor on inactive   InputConnection W / IInputConnectionWrapper:getCursorCapsMode on   无效的InputConnection W / IInputConnectionWrapper:getCursorCapsMode   在非活动的InputConnection上

1 个答案:

答案 0 :(得分:0)

这是您的创建表查询的问题。

String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
        + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + "TEXT,"+ KEY_PRICE + "INTEGER,"
        + KEY_START + " TEXT," + KEY_MANHOURS + "INTEGER,"
        + KEY_FINISH + " TEXT" + ")";

将此更新为

String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
        + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"+ KEY_PRICE + " INTEGER,"
        + KEY_START + " TEXT," + KEY_MANHOURS + " INTEGER,"
        + KEY_FINISH + " TEXT" + ")"; 

尝试找出column_name和column_type之间缺少的空格。