HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinorCanonical

SQLite Helper Class in C#

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
helpersqliteclass

Problem

Here is a SQLite Helper Class I created. Any input appreciated, including security and simplification.

```
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SQLiteHelper
{
public class EntryList
{
public EntryList()
{
ColumnName = new List();
DbType = new List();
Content = new List();
}
public List ColumnName { set; get; }
public List DbType { set; get; }
public List Content { set; get; }

}
public class ListWithName
{
public ListWithName()
{
SubItems = new List();
}
public string Text { set; get; }
public List SubItems { set; get; }
}
public class ColumnProperties
{
public string ID { get; set; }
public string Name { get; set; }
public string DataType { get; set; }
public string Content { get; set; }
public bool AllowNull { get; set; }
}
public class Column : IEnumerable
{
public Column()
{
cols = new List();
}
private List cols = new List();
public IEnumerator GetEnumerator()
{
return this.cols.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(string Name)
{
ColumnProperties ml = new ColumnProperties();
ml.Name = Name;
ml.DataType = "VARCHAR";
ml.AllowNull = true;
cols.Add(ml);
}
public void Add(string Name, string DataType)
{
ColumnProperties ml = new ColumnProperties();
ml.Name = Name;
ml.DataType = DataType;
ml.AllowNull =

Solution

Some things that jump at me for the first look.

-
Your DataType should definitely not be a String but some Enum. This makes passing invalid arguments to the function almost impossible. something like the following should do:

public enum DbDataTypes{
VARCHAR, TIMESTAMP, LONG, INT, DOUBLE, DATETIME, [...]
}


-
I personally prefer to have the overloaded methods with less parameters call the higher overloaded ones:

public void Add(string name){
Add(name, DbDataType.VARCHAR, true);
}


-
The singular in Column class makes one think you have only one column, when in fact you have several. Chose a more speaking name like ColumnList

-
I don't like that you add the ID column as primary key for every table you create. Mapping tables as the most common example often do not have a column named ID. They also usually have a combined primary key.

-
You don't check the size of the passed arrays in your CreateTable method. This may lead to difficult to debug IndexOutOfRange exceptions

-
you might want to refactor your entryList. it probably will be better to have:

public class EntryList : IList {

}
public class Entry{
public string ColumnName {get; set;}
public DbType DataType {get; set;}
public string Value {get; set;}
}


that way you ensure to always have a corresponding number of ColumnName, DataType and Value.

-
Also you should chose one naming convention to follow. some of your variables are in camelCase some in MixedCase.

-
Finally you really should move this to multiple files. You could create partial classes to have some more overview of what functionality is where. Luckily, as opposed to java, the filename can be different from the classname ;)

Context

StackExchange Code Review Q#42356, answer score: 8

Revisions (0)

No revisions yet.