gpt4 book ai didi

java - 我的表没有在 Sqlite 中创建(Toast for dataNotInserted)....下面的代码 fragment

转载 作者:行者123 更新时间:2023-12-02 11:28:09 25 4
gpt4 key购买 nike

我的表没有在 Sqlite 中创建(Toast for dataNotInserted)。下面的代码 fragment ...

异常

android.database.sqlite.SQLiteException: no such table: PETROL.DB (code 1): , while compiling: INSERT INTO PETROL.DB(AMOUNT) VALUES (?)

Activity2.java

public class Activity2 extends AppCompatActivity {
EditText ET1;
Button B1;
Button Bview;
DatabaseHelper myDb;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);


myDb = new DatabaseHelper(this);
ET1 = findViewById(R.id.editText_amount);
B1 = findViewById(R.id.button_add);
Bview = findViewById(R.id.button_view);
AddData();

}


public void AddData() {
B1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean isInserted = myDb.insertData(Integer.parseInt(ET1.getText().toString()));
if (isInserted == true) {
Toast.makeText(Activity2.this, "DataInserted", Toast.LENGTH_LONG).show();
} else
Toast.makeText(Activity2.this, "DataNotInserted", Toast.LENGTH_LONG).show();
}
}
);
}
}

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME="PETROL.DB";
public static final String TABLE_NAME="PETROL_TABLE";
public static final String COL_1="ID";
public static final String COL_2="AMOUNT";

public DatabaseHelper(Context context)
{
super(context,DATABASE_NAME,null,1);
}


@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {

sqLiteDatabase.execSQL( "CREATE TABLE " + TABLE_NAME + "(" + COL_1 + " INTEGER PRIMARY KEY AUTOINCREMENT," + COL_2 + " INTEGER );");

Log.d("data","ghgc");
}

public boolean insertData(int data)
{
SQLiteDatabase sqLiteDatabase=this.getWritableDatabase();
ContentValues contextValues=new ContentValues();
contextValues.put("AMOUNT",data);
Log.d("insertData",Integer.toString(data));
long result= sqLiteDatabase.insert(DATABASE_NAME,null,contextValues);
if(result==-1)
return false;//to check if data is inserted
else
return true;
//sqLiteDatabase.close();

}
}

未提及 onUpgrade,因为我仍在开发版本 1

最佳答案

您的问题与消息一致,是由于您将数据库名称 PETROL.DB 传递给 insert 方法而不是传递表名称而引起的。根据方法的签名:-

long insert(String table, String nullColumnHack, ContentValues values)

Convenience method for inserting a row into the database. SQLiteDatabase - insert

因此改变:-

    long result= sqLiteDatabase.insert(DATABASE_NAME,null,contextValues);

至:-

    long result= sqLiteDatabase.insert(TABLE_NAME,null,contextValues);

关于java - 我的表没有在 Sqlite 中创建(Toast for dataNotInserted)....下面的代码 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49468573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com