我是这个android stuido编程的新手,我希望你会 帮助我解决这个问题。我知道你之前回答了这个问题 但是您显示的所有方法都无济于事。我有数据库,当我 想添加一些显示错误的信息,提示我没有命名列 数据库。如果您能帮助我,我将不胜感激。
enter code here package algebra.hr.order;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBase extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Order2.db";
public static final String TABLE_NAME = "users_data";
public static final String COL1 = "ID";
public static final String COL2 = "broj";
public static final String COL3 = "hrana";
public static final String COL4 = "piče";
public static final String COL5 = "vrijeme";
public DataBase(Context context) {
super(context, DATABASE_NAME, null, 3);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + "(" +
"ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 + "TEXT, " + COL3 + "TEXT, " + COL4 + "TEXT, " + COL5 +
"TEXT"
+ ");";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String broj, String hrana, String piče , String
vrijeme) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, broj);
contentValues.put(COL3, hrana);
contentValues.put(COL4, piče);
contentValues.put(COL5 , vrijeme);
float result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1) {
return false;
} else {
return true;
}
}
public Cursor getListContents() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
return data;
}
}
enter code here package algebra.hr.order;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main2Activity extends AppCompatActivity {
Spinner hrana , piće;
EditText soba;
Button btn1, btn2;
String HRANA = "";
String PIČE = "";
String broj ="";
String vrijeme = "";
Date d;
DataBase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main2 );
hrana = (Spinner)findViewById ( R.id.Odabirhrane );
piće = (Spinner)findViewById ( R.id.Odabirpića );
soba = (EditText)findViewById ( R.id.brojSobe );
btn1 = (Button)findViewById ( R.id.btnNatuci );
btn2 = (Button)findViewById ( R.id.povjest );
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource (
this , R.array.Hrana, android.R.layout.simple_spinner_item );
hrana.setAdapter ( adapter );
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource (
this, R.array.Pića , android.R.layout.simple_spinner_item);
piće.setAdapter ( adapter1 );
btn1.setOnClickListener ( new View.OnClickListener () {
@Override
public void onClick(View v) {
broj = soba.getText ().toString ();
HRANA = hrana.getSelectedItem ().toString ();
PIČE = piće.getSelectedItem ().toString ();
db = new DataBase ( Main2Activity.this );
d = new Date ( );
SimpleDateFormat sdf = new SimpleDateFormat ( "hh:mm a" );
vrijeme = sdf.format ( d );
if (broj.length () != 0 && HRANA.length () != 0 && PIČE.length
() != 0 && vrijeme.length () != 0) {
adddata ( broj , HRANA , PIČE , vrijeme );
} else {
Toast.makeText ( Main2Activity.this , "Niste ništa naručili"
, Toast.LENGTH_SHORT ).show ();
}
}
} );
btn2.setOnClickListener ( new View.OnClickListener () {
@Override
public void onClick(View v) {
Intent i = new Intent ( Main2Activity.this , DataView.class );
startActivity ( i );
}
} );
}
public void adddata( String b , String h ,String p ,String v){
boolean insertData = db.addData ( b , h , p , v);
if(insertData==true){
Toast.makeText(Main2Activity.this,"Uspješno ste
naručili.",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(Main2Activity.this,"Nešto je pošlo po zlu :
(.",Toast.LENGTH_LONG).show();
}
}
}
答案 0 :(得分:0)
也许您只需要在像这样创建sqlite的数据类型中添加空间
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + "(" +
"ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 + " TEXT, " + COL3 + " TEXT, " + COL4 + " TEXT, " + COL5 +
" TEXT"
+ ");";
db.execSQL(createTable);
}