0

这是数据库

    package com.example.sama;

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

public class Database extends Activity  {

public static final String KEY_ROWID ="_id";
public static final String KEY_NAME ="Bills_name";
public static final String KEY_MAXAMOUNT ="_";
public static final String KEY_REMAIN ="what_is_left";
private static final String DATABASE_NAME ="SAMA";
private static final String DATABASE_TABLE ="gg";
private static final int DATABASE_VERSION = 1;
private DBhelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;



public 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_NAME + " TEXT NOT NULL, " +
        KEY_MAXAMOUNT + " INTEGER NOT NULL , " +
        KEY_REMAIN + " INTEGER );"
    );
    }

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

    }



}

public Database(Context c){
ourContext = c ;

}
public Database open() throws SQLException 
{
ourHelper = new DBhelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;

}
public void close(){
ourHelper.close();
}
public long creatEntry(String name, String maxamount) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_MAXAMOUNT, maxamount);
return ourDatabase.insert(DATABASE_TABLE, null, cv);

}
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID, KEY_NAME ,KEY_MAXAMOUNT,KEY_REMAIN };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result ="";
int iROW =c.getColumnIndex(KEY_ROWID);
int iNAME =c.getColumnIndex(KEY_NAME);
int iMAXAMOUNT =c.getColumnIndex(KEY_MAXAMOUNT);
int iREMAIN =c.getColumnIndex(KEY_REMAIN);

for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
    result = result + c.getString(iROW) + "  " + c.getString(iNAME) + "  " +c.getString(iMAXAMOUNT) + "  " + c.getString(iREMAIN) +"\n ";

}

return result;
}


}

这是新的字段代码,当我尝试从我正在创建的应用程序向数据库中添加一些内容时,它说它无法创建表,而代码假设是自己创建表的任何人都知道出了什么问题,谢谢你时间

`package com.example.sama;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class newfield extends Activity implements OnClickListener   {
Button save,cancel;
EditText maxAmount,FieldName; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newfield);
    maxAmount = (EditText) findViewById(R.id.maxAmount);
    FieldName = (EditText) findViewById(R.id.FieldName);
    save = (Button) findViewById(R.id.SQLsave);
    cancel = (Button) findViewById(R.id.SQLcancel);
    save.setOnClickListener(this);
    cancel.setOnClickListener(this);
}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch (arg0.getId()){
    case R.id.SQLsave:
          boolean didItWork = true  ;
             try{
            String name = FieldName.getText().toString();
            String maxamount = maxAmount.getText().toString();

            Database entry = new Database(newfield.this);
            entry.open();
            entry.creatEntry(name, maxamount);
            entry.close();
            }catch (Exception e ){
                didItWork = false ;
                Dialog d = new Dialog(newfield.this );
                d.setTitle("doneeee !");
                TextView tv =new TextView(newfield.this);
                tv.setText("sucsses");
                d.setContentView(tv);
                d.show();
            }finally{
                if (didItWork){
                    Dialog d = new Dialog(newfield.this );
                    d.setTitle("done !");
                    TextView tv =new TextView(newfield.this);
                    tv.setText("sucsses");
                    d.setContentView(tv);
                    d.show();
                    Intent i =  new Intent  (newfield.this,MainActivity.class);
                    startActivity(i);                       

                }
            }

        break;
    case R.id.SQLcancel:
        finish();
        break;
    }

}

}
4

1 回答 1

0

问题可能出在 的定义上KEY_MAXAMOUNT = "_"。我不确定只是一个下划线是一个有效的列名。这将导致CREATE TABLE失败。尝试为该列使用不同的名称。

于 2013-05-07T07:47:30.930 回答