我正在创建一个简单的预算管理应用。为了存储预算详细信息,我创建了一个表格。这是我的DataBaseHelper类:
package com.example.rajkumar.budget;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION=1;
private static final String DATABASE_NAME="SQLiteDatabase.db",
TABLE_NAME="MyBudget";
public static final String COLUMN_FIRST_NAME="NAME",
COLUMN_TWO_NAME="BUDGET_NAME",
COLUMN_THREE_NAME="LIMIT";
public DataBaseHelper(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db){
db.execSQL("CREATE TABLE" + TABLE_NAME + "(" + COLUMN_FIRST_NAME + "VARCHAR," + COLUMN_TWO_NAME
+ "VARCHAR," + COLUMN_THREE_NAME + "DECIMAL);");
}
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
db.execSQL("DROP TABLE IF EXISTS"+TABLE_NAME);
onCreate(db);
}
public boolean addBudget(String name,String bname,String bLimit){
SQLiteDatabase db=getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(COLUMN_FIRST_NAME,name);
contentValues.put(COLUMN_TWO_NAME,bname);
contentValues.put(COLUMN_THREE_NAME, bLimit);
db.insert(TABLE_NAME, null, contentValues);
db.close();
return true;
}
public Cursor getDetails(){
SQLiteDatabase db= getReadableDatabase();
Cursor c=db.rawQuery("select *from MyBudget;",null);
return c;
}
}
下面是一个类,从中提供值来创建表。
package com.example.rajkumar.budget;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class BudgetDetails extends AppCompatActivity {
EditText editName,editBname,editLimit;
Button btnCreate;
private TextView budgetName,name,limit;
private DatabaseHelper db;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.enter_budget);
editName=(EditText)findViewById(R.id.editText);
editBname=(EditText)findViewById(R.id.editText2);
editLimit=(EditText)findViewById(R.id.editText3);
btnCreate=(Button)findViewById(R.id.buttonC);
btnCreate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
insert();
}
});
setContentView(R.layout.budget_details);
budgetName = (TextView) findViewById(R.id.textViewbname);
name = (TextView) findViewById(R.id.textViewname);
limit = (TextView) findViewById(R.id.textViewlimit);
db = new DatabaseHelper(this);
showDetails();
Button btndel = (Button) findViewById(R.id.button2);
btndel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(BudgetDetails.this, "Delete later", Toast.LENGTH_LONG).show();
}
});
}
private void insert(){
String name=editName.getText().toString().trim();
String bname=editBname.getText().toString().trim();
String blimit=editLimit.getText().toString().trim();
db.addBudget(name,bname,blimit);
Toast.makeText(this,"Budget Created Successfully",Toast.LENGTH_LONG).show();
}
private void showDetails(){
Cursor c=db.getDetails();
c.moveToFirst();
budgetName.setText(c.getString(c.getColumnIndex(DatabaseHelper.COLUMN_FIRST_NAME)));
name.setText(c.getString(c.getColumnIndex(DatabaseHelper.COLUMN_TWO_NAME)));
limit.setText(c.getString(c.getColumnIndex(DatabaseHelper.COLUMN_THREE_NAME)));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()){
case R.id.home:
setContentView(R.layout.activity_main);
return(true);
case R.id.exit:
finish();
return(true);
case R.id.settings:
Toast.makeText(this, "Settings Selected", Toast.LENGTH_LONG).show();
return(true);
}
return super.onOptionsItemSelected(item);
}
}
主要活动包括一个网格视图,我想点击一个项目并打开一个布局来输入预算详细信息并将其保存在一个表格中,并在单独的布局中显示详细信息。该应用程序运行良好,直到我点击一个项目然后它显示"不幸的是(myapp)已停止"。下面是我的MainActivity代码
package com.example.rajkumar.budget;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
GridView grid;String[] budget= {"Quick","Budget","Expenses","Reports","Tips","About"};
int[] ImageId= {R.drawable.bookmark,R.drawable.bookshelf,R.drawable.briefcase,R.drawable.candy,
R.drawable.car,R.drawable.clock};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomGrid adapter=new CustomGrid(MainActivity.this,budget,ImageId);
grid=(GridView)findViewById(R.id.grid);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i=new Intent(MainActivity.this,BudgetDetails.class);
Toast.makeText(MainActivity.this, "You Clicked at " + position, Toast.LENGTH_LONG).show();
i.putExtra("id",position);
startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()){
case R.id.home:
return(true);
case R.id.exit:
finish();
return(true);
case R.id.settings:
Toast.makeText(this,"Settings Selected",Toast.LENGTH_LONG).show();
return(true);
}
return super.onOptionsItemSelected(item);
}
}
这里是logcat错误消息:
11-30 05:26:06.469 1605-1605/com.example.rajkumar.budget W/dalvikvm﹕ VFY: nable to resolve virtual method 369: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
11-30 05:26:06.469 1605-1605/com.example.rajkumar.budget D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
11-30 05:26:06.639 1605-1605/com.example.rajkumar.budget E/SQLiteLog﹕ (1) near "TABLEMyBudget": syntax error
11-30 05:26:06.639 1605-1605/com.example.rajkumar.budget D/AndroidRuntime﹕ Shutting down VM
11-30 05:26:06.679 1605-1605/com.example.rajkumar.budget W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb0c70b20)
11-30 05:26:06.679 1605-1605/com.example.rajkumar.budget E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.rajkumar.budget, PID: 1605
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rajkumar.budget/com.example.rajkumar.budget.BudgetDetails}: android.database.sqlite.SQLiteException: near "TABLEMyBudget": syntax error (code 1): , while compiling: CREATE TABLEMyBudget(NAMEVARCHAR,BUDGET_NAMEVARCHAR,LIMITDECIMAL);
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
答案 0 :(得分:5)
在CREATE
语句中的变量前后放置一个空格:
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + COLUMN_FIRST_NAME + " VARCHAR," + COLUMN_TWO_NAME
+ " VARCHAR," + COLUMN_THREE_NAME + " DECIMAL);")
您还必须以相同的方式调整其他查询(例如DROP
查询)