我是SQL新手,希望我们能帮助我,谢谢你!
我收到此错误
replace
我的logcat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ztl.ztlubricant.materialtest/com.ztl.ztlubricant.materialtest.MainActivity}: android.database.sqlite.SQLiteException: near "_productname": syntax error (code 1): , while compiling: CREATE TABLE products(_id INTEGER PRIMARY KEY AUTOINCREMENT _productname TEXT );
这是我的MainActivity
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: FATAL EXCEPTION: main
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: Process: com.ztl.ztlubricant.materialtest, PID: 3800
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ztl.ztlubricant.materialtest/com.ztl.ztlubricant.materialtest.MainActivity}: android.database.sqlite.SQLiteException: near "_productname": syntax error (code 1): , while compiling: CREATE TABLE products(_id INTEGER PRIMARY KEY AUTOINCREMENT _productname TEXT );
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5021)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: Caused by: android.database.sqlite.SQLiteException: near "_productname": syntax error (code 1): , while compiling: CREATE TABLE products(_id INTEGER PRIMARY KEY AUTOINCREMENT _productname TEXT );
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at com.ztl.ztlubricant.materialtest.MyDBHandler.onCreate(MyDBHandler.java:29)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at com.ztl.ztlubricant.materialtest.MyDBHandler.databaseToString(MyDBHandler.java:55)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at com.ztl.ztlubricant.materialtest.MainActivity.printDatabase(MainActivity.java:45)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at com.ztl.ztlubricant.materialtest.MainActivity.onCreate(MainActivity.java:28)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5231)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5021)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
12-05 12:09:40.251 3800-3800/? E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
我的处理程序
public class MainActivity extends AppCompatActivity {
EditText zubInput;
TextView zubText;
MyDBHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
zubInput = (EditText) findViewById(R.id.zubName);
zubText = (TextView) findViewById(R.id.textView);
dbHandler = new MyDBHandler(this, null, null, 1);
printDatabase();
}
public void add(View view) {
Products product = new Products(zubInput.getText().toString());
dbHandler.addProduct(product);
printDatabase();
}
public void delete(View view) {
String inputText = zubInput.getText().toString();
dbHandler.deleteProduct(inputText);
printDatabase();
}
public void printDatabase() {
String dbString = dbHandler.databaseToString();
zubText.setText(dbString);
zubInput.setText(" ");
}
@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.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
并且
public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "products.db";
public static final String TABLE_PRODUCT = "products";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_PRODUCTNAME = "_productname";
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_PRODUCT + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT " +
COLUMN_PRODUCTNAME + " TEXT " +
");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_PRODUCT);
onCreate(db);
}
public void addProduct(Products product){
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCTNAME, product.get_productname());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_PRODUCT, null, values);
db.close();
}
public void deleteProduct(String productName){
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM" + TABLE_PRODUCT + "WHERE" + COLUMN_PRODUCTNAME + "=\"" + productName + "\";");
}
public String databaseToString() {
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM" + TABLE_PRODUCT + "WHERE 1";
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
while (!c.isAfterLast()) {
if(c.getString(c.getColumnIndex("productname")) != null) {
dbString += c.getString(c.getColumnIndex("productname"));
dbString += "\n";
}
}
db.close();
return dbString;
}
}
尽量提供尽可能多的信息..帮助我!
答案 0 :(得分:0)
SQL
create table
语法错误。
CREATE TABLE products(_id INTEGER PRIMARY KEY AUTOINCREMENT _productname TEXT );
您错过了逗号以分隔每列(此处,在_id
列之后,您应添加,
。)。
CREATE TABLE products(_id INTEGER PRIMARY KEY AUTOINCREMENT, _productname TEXT );